Website Summary Files Commits Branches @commit @diff @stats
path: root / Utils / SI_K1 / task2.cpp
blob: 328a1fbe20c08cccbf063e0d39b15feb45403891 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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;
}