String To Integer and Integer to String Conversion in C
String to Integer Conversion
int Convert(char input[]) { int sum = 0, i, p = 1, digit; for(i = strlen(input) - 1; i>=0; i--) { digit = input[i] - '0'; sum += digit*p; p *= 10; } return sum; }
Integer to String Conversion
Integer to string conversion is reverse of string to integer conversion described above. In this process, each digit is extracted from the integer and character ‘0’ is added to make it char. At last, all the digits that are converted to char are combined to make string. A portion of C code is
void IntToString(int n) { int k = 0; if(n == 0) { return 0; } k = IntToString(n/10); string[k] = n%10 +'0'; }
You must add an end of line ‘’ character at the end of the converted string.
your function must have an integer return type. and only the first character is updated every time. please check…