0

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');
}
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
Registered User
  • 1,554
  • 3
  • 22
  • 37
  • What you call 'digit' everybody else calls a 'number'. (0 to 9 are digits). And `system("\npause")` will not work, because there is no command named "\npause". Use `system("pause")` instead, or better yet, don't start your program from a GUI but from a command line. – Jens Jan 30 '12 at 13:36
  • Possible duplicate of [Determine if char is a num or letter](http://stackoverflow.com/questions/8611815/determine-if-char-is-a-num-or-letter) – Ciro Santilli OurBigBook.com Mar 11 '16 at 09:25

4 Answers4

1
  1. You're dealing with a string. Just iterate until you find '\0'. No need to get weird with sizeof.
  2. Read man isdigit
  3. If you really want to pass a pointer to scanf, it needs to be &c[0]. However, c all by its lonesome is a pointer so you can just say scanf("%s", c);
  4. And that state variable. Try running some values through by hand on paper and see what happens to it.
Andrew Beals
  • 1,177
  • 8
  • 18
0
arr_size=(sizeof(c)/sizeof(c[0]));
    //arr_size=30/1

for(i; i < arr_size; i++)
    //for(i; i < 30; i++)

Wouldn't state be a "no" if you enter a number less than 30digits... If you enter a 30 digit number like this: 12234**************8877, wouldn't "7" the last digit of the number make stats a yes?

use if(isdigit()) and for(i; i < strlen(c); i++) and a break whenever state becomes no..

Lucas Zamboulis
  • 2,494
  • 5
  • 24
  • 27
0

Your for loop runs over every character in the array setting state each iteration. Each time state is set, the previous value is forgotten.

Thus your for loop is actually equivalent to writing

state = is_digit(c[29]);

You need to make two changes:

  1. Only iterate over the character array that the user entered. So stop iterating when you encounter the null-terminating character.
  2. Set state to TRUE only if all characters are digits.

Since this looks like homework, I won't write the code out in full for you!

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
0

You always override state, so it is checking only if the last element is a digit.

    if(check_digit(c[i]))
    state = yes;
    else
    state = no;

use instead: state = (state_digit(c[i]) && state) [and init state to be yes]: It means, you are looking for one entry of "no digit" and once you find it, you propgate the answer.

Another possibility is just breaking once you find a non-digit.

amit
  • 175,853
  • 27
  • 231
  • 333