-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring_to_number.c
101 lines (100 loc) · 1.95 KB
/
string_to_number.c
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <stdio.h>
#include <stdlib.h>
void num_to_string(int result, int len, char *new);
char int_to_char(int inte);
int str_to_num(char * str);
int convert_to_int(char element);
int main(int argc, char* argv[])
{
int j;
int result;
char str1[]="11";
char str2[]="12";
result = str_to_num(str1)+str_to_num(str2);
for (int i =result; i>0; i=i/10)
{
j++;
}
j++;
char new[j];
num_to_string(result,j,new);
printf("%s",new);
}
char int_to_char(int inte)
{
switch(inte)
{
case 0:
return '0';
case 1:
return '1';
case 2:
return '2';
case 3:
return '3';
case 4:
return '4';
case 5:
return '5';
case 6:
return '6';
case 7:
return '7';
case 8:
return '8';
case 9:
return '9';
}
}
void num_to_string(int result, int len, char *new)
{
int j;
for(int i = result,j=len-2; i>0; i=i/10,j--)
{
new[j]=int_to_char(i%10);
}
new[len-1]='\0';
}
int convert_to_int(char element)
{
switch(element)
{
case '0':
return 0;
case '1':
return 1;
case '2':
return 2;
case '3':
return 3;
case '4':
return 4;
case '5':
return 5;
case '6':
return 6;
case '7':
return 7;
case '8':
return 8;
case '9':
return 9;
}
}
int str_to_num(char * str)
{
int count;
int multiplier=1;
int running_sum = 0;
for (count = 0; str[count] != '\0'; count++);
for(int i = 0; i<count-1; i++)
{
multiplier=multiplier*10;
}
for( int j =0; j <count ; j++)
{
running_sum = running_sum+(multiplier*convert_to_int(str[j]));
multiplier = multiplier/10;
}
return running_sum;
}