-
isdigit
Is the function isdigit able to find digits that are in a string?
For example if I had a character string named string that contained the characters 34678904.
would
return a 0 since the character is a digit or would it return a nonzero b/c it recognizes it as something else?
If it returns a nonzero, what is a good way to check if a string is made up of characters or digits?
-
isdigit returns true if a character is one of 0123456789. You can check to see if a string consists solely of digits by looping over every element:
Code:
int anumber(char *s) {
while (*s != '\0') {
if (!isdigit(*s++))
return 0;
}
return 1;
}
-
>Is the function isdigit able to find digits that are in a string?
Yes.
>would
> isdigit(string)
>return a 0
No. The function isdigit is used on a single character, not a string.
Code:
#include <stdio.h>
#include <ctype.h>
int main(void)
{
const char text[] = "34678904 plus other stuff !5@#$";
size_t i;
for ( i = 0; i < sizeof text - 1; ++i )
{
if ( isdigit(text[i]) )
{
putchar(text[i]);
}
}
putchar('\n');
return 0;
}
/* my output
346789045
*/
>what is a good way to check if a string is made up of characters or digits?
If this is in the process of trying to convert a string representation of a number into an integer value, this may help.
-
thnaks guys you have been a big help