I’m trying to check whether the entered chars are digits or not with pattern checking.
I’ve written following program.
This program not giving me the perfect output for following test cases
can anybody tell me where I’m going wrong with my logic.
/*
Output test case
234234 = It's a digit.
a3434a = It's not a digit.
33aa3a = It' not a digit.
*/
#define yes 1
#define no 0
#include <stdio.h>
int main(void)
{
char c[30];
int arr_size, result, i=0, state;
printf("Enter your digit:= ");
scanf("%s",&c);
arr_size=(sizeof(c)/sizeof(c[0]));
for(i; i < arr_size; i++)
{
if(check_digit(c[i]))
state = yes;
else
state = no;
}
if(!state)
printf("It's not a digit\n");
else
printf("It's a digit\n");
system("\npause");
return 0;
}
int check_digit(char c)
{
return (c>='0' && c<='9');
}
sizeof.man isdigit&c[0]. However,call by its lonesome is a pointer so you can just sayscanf("%s", c);statevariable. Try running some values through by hand on paper and see what happens to it.