8. Write a function that takes in a string parameter and checks to see whether or not it is an integer, and if it is then return the integer value.


8. Write a function that takes in a string parameter and checks to see whether or not it is an integer, and if it is then return the integer value.

#include <stdio.h>
 int strtoint(char *s)
{
 int index = 0, flag = 0;

while( *(s+index) != '\0')
if( (*(s + index) >= '0') &&  *(s + index) <= '9')
{
 flag = 1; index++;
}
 else
{
 flag = 0;
 break;
}
}
if( flag == 1 ) return atoi(s);
else return 0;
}
 main() 
{
  printf("%d",strtoint("0123"));  printf("\n%d",strtoint("0123ii"));  
}


Disqus Comments