An internal implementation method based on atoi of and itoa of functions
- 2020-04-01 23:44:49
- OfStack
The C language provides several standard library functions that convert Numbers of any type (integer, long integer, floating point, etc.) into strings. The following is to use
An example of the itoa() function converting an integer toa string:
atoi Converts a string to an integer
itoa Converts an integer to a string
atoi Converts a string to an integer
itoa Converts an integer to a string
#include "stdio.h"
#include "ctype.h"
#include "stdlib.h"
int my_atoi(char s[])
{
int i,n,sign;
for(i=0;isspace(s[i]);i++); //Skip the blank
sign=(s[i]=='-')?-1:1;
if(s[i]=='+'||s[i]==' -') //Skip sign bit
i++;
for(n=0;isdigit(s[i]);i++)
n=10*n+(s[i]-'0'); //Converts numeric characters into plastic Numbers
return sign*n;
}
void my_itoa(int n,char s[])
{
int i,j,sign;
if((sign=n)<0) //Recording symbols
n=-n; //Make n positive
i=0;
do{
s[i++]=n%10+'0'; //Take the next number
}while((n/=10)>0); //Cycle division
if(sign<0)
s[i++]='-';
s[i]='0';
for(j=i-1;j>=0;j--) //The generated Numbers are in reverse order, so output in reverse order
printf("%c",s[j]);
}
void main()
{
int n;
char str[100];
my_itoa(-123,str);
printf("n");
printf("%dn",my_atoi("123"));
system("pause");
}