Encoders
Hub for Computer Whizzes Register to join us

Encoders > More... > C > Program

/* Function atoi(s) to convert a string into number after skipping the white
   spaces in the beginning and terminating the conversion when first
   non-digit character is encountered. */


#include <stdio.h>

int atoi(char string[]);

main()
{
	char a[100];
	gets(a);
        printf("%d\n",atoi(a));
}
int atoi(char s[])
{
	int i,n,sign=1;
	for(i=0; s[i]==' ' || s[i]=='\t' || s[i]=='\n'; i++)
		;
	if(s[i]=='+'||s[i]=='-')
		sign = s[i++]=='-'?-1:1;
        for(n=0; s[i]>='0' && s[i]<='9'; i++)
		n = n*10 + s[i]-'0';
	return sign*n;
}