From 16733e4dd72ec23bd9707c38bdf19086c1fd8769 Mon Sep 17 00:00:00 2001 From: Angel Dimitriev Date: Sun, 5 Dec 2021 03:20:40 -0500 Subject: Create task2.cpp --- Utils/SI_K1/task2.cpp | 85 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 Utils/SI_K1/task2.cpp (limited to 'Utils/SI_K1/task2.cpp') diff --git a/Utils/SI_K1/task2.cpp b/Utils/SI_K1/task2.cpp new file mode 100644 index 0000000..7d0230f --- /dev/null +++ b/Utils/SI_K1/task2.cpp @@ -0,0 +1,85 @@ +#include +#include +#pragma warning (disable:4996) +using namespace std; + +const unsigned CAPACITY = 1024; + + +void task2a(const char* text, unsigned& upperCase, unsigned& lowerCase) +{ + while(*text != '\0') + { + if('a' <= *text && *text <= 'z') + lowerCase++; + + if('A' <= *text && *text <= 'Z') + upperCase++; + + text++; + } +} + + +bool isDigit(char ch) +{ + return ch >= '0' && ch <= '9'; +} + + +//we assume that ch is a digit char. +int convertToInt(char ch) +{ + return ch - '0'; +} + +unsigned task2b(char* text) +{ + char newText[CAPACITY]; + unsigned newTextIter = 0; + + char* textIter = text; + + unsigned mult = 1; //would be 0 for var2 + unsigned accumulatedNumber = 0; + + while (*textIter != '\0') + { + if (isDigit(*textIter)) + { + (accumulatedNumber *= 10) += convertToInt(*textIter); + textIter++; + } + else + { + if(accumulatedNumber != 0) + mult *= accumulatedNumber; // += for var2 + accumulatedNumber = 0; + + newText[newTextIter++] = *textIter; + textIter++; + } + + } + + //if the string ends with a digit we should add it too. + if(accumulatedNumber != 0) + mult *= accumulatedNumber; // += for var2 + + newText[newTextIter] = '\0'; + strcpy(text, newText); + return mult; +} + + +int main() +{ + char text[CAPACITY] = "Abc23ffds3ff5"; + + unsigned lowerCase = 0, upperCase = 0; + task2a(text, upperCase, lowerCase); + cout << lowerCase << " " << upperCase << endl; + + cout << task2b(text) << endl; + cout << text << endl; +} -- cgit v1.3.1-23-gd82d