1#include <stdio.h>2 intStr_to_int (Const Char*STR,int*num);3 voidINT_TO_STR (CharStr[],Const intStrLen,Const intnum);4 5 intMainintargcChar*argv[])6 {7 Charstr[ -];8 intRET =0, n =0;9 while(1)Ten { Onescanf"%s", str); Aret = Str_to_int (str,&n); - if(ret = =0) - { theprintf"%d\n", n); -Int_to_str (str, -, n); -printf"%s\n", str); - } + Else - { +printf"error\n"); A } at } - return 0; - } - - intStr_to_int (Const Char*STR,int*num) - { in intSign =1; -*num =0; to Const Char*p =str; + while(*p! =' /') - { the if(*p >='0'&& *p <='9' ) * { $*num = *num *Ten+ *p-'0';Panax Notoginsengp++; - } the Else if(*p = ='-'&& *num = =0) + { ASign =-1; thep++; + } - Else $ { $ return-1; - } - } the*num *=Sign ; - return 0;Wuyi } the - voidINT_TO_STR (CharStr[],Const intStrLen,Const intnum) Wu { - intTMP =0, n =0, i =0; About if(Num <0) $ { -n =-num; -i =1; - } A while(n! =0) + { theTMP = TMP *Ten+ n%Ten; -N/=Ten; $ } the while(I<strlen && tmp! =0) the { thestr[i++] = tmp%Ten+'0'; theTMP/=Ten; - } in}
Conversion of C code string to Integer