convert char digits to integer digits

How do i convert char digits to normal integer digits

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
int main()
{
	char str [MAX];
	int n, pair;
	
	srand(time(NULL));
	
	n = 2* (rand() % 10 + 4);

	constructArray(str, n);
	printArray(str, n);
	cout << largestPair(str);
	
	

}

void constructArray(char str[], int n)
{
	char digits [] = {'1', '2', '3', '4', '5', '6', '7', '8', '9', '0'};
	
	int k;
	for (int i = 0; i < n; i++)
	{	 
		k = rand() % 10;
		str [i] = digits[k];
	
		
		
		if (str[0] == '0')
		{
			str[0] = digits[k] + 1; //Ensures 1st digit > 0
		}

	}

}

void printArray (const char str [], int n)
{
	cout << "Given ";
	for (int i = 0; i < n; i++)
		cout << str [i];
	cout << endl; 
	
	
}


Right now my program generates a random string of digits, I need to convert those char digits to integer digits for some calculations and comparisons.
1
2
3
int  charToInt( char c ) { return c - '0'; }

char intToChar( int i  ) { return (char) ( '0' + i ); }


Rather basic routines. No checking and digits 0-9 only.
Last edited on
Topic archived. No new replies allowed.