I want to check if a string is a number with this code. I must check that all the chars in the string are integer, but the while returns always isDigit = 1. I don\'t know wh
#include
#include
char isNumber(char *text)
{
int j;
j = strlen(text);
while(j--)
{
if(text[j] > 47 && text[j] < 58)
continue;
return 0;
}
return 1;
}
int main(){
char tmp[16];
scanf("%s", tmp);
if(isNumber(tmp))
return printf("is a number\n");
return printf("is not a number\n");
}
You can also check its stringfied value, which could also work with non Ascii
char isNumber(char *text)
{
int j;
j = strlen(text);
while(j--)
{
if(text[j] >= '0' && text[j] <= '9')
continue;
return 0;
}
return 1;
}