Website Summary Files Commits Branches @commit @diff @stats
diff options
context:
space:
mode:
Commit16733e4dd72ec23bd9707c38bdf19086c1fd8769 (patch)
Author
 Angel Dimitriev <achi_d@abv.bg>
2021-12-05 03:20:40 -0500
Committer
 GitHub <noreply@github.com>
2021-12-05 03:20:40 -0500
Files11ae2e638a90cd66543af4d6208e2b06e1df7d0b
Introduction_to_programming_FMI-16733e4dd72ec23bd9707c38bdf19086c1fd8769.tar
Introduction_to_programming_FMI-16733e4dd72ec23bd9707c38bdf19086c1fd8769.tar.gz
Introduction_to_programming_FMI-16733e4dd72ec23bd9707c38bdf19086c1fd8769.zip
Parent50d6222d2c9b2426cbcf7f61afbd60610aeb0fc9 (diff)
Create task2.cpp
1 files changed, 85 insertions, 0 deletions
-rw-r--r--Utils/SI_K1/task2.cpp85
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 <iostream>
+#include <cstring>
+#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;
+}