Thread: I am having problems with isdigit, isalpha, isupper.

  1. #1
    Registered User
    Join Date
    Oct 2015
    Posts
    1

    Angry I am having problems with isdigit, isalpha, isupper.

    this is what I am trying to do :
    Code:
    int a = ' ';
       printf("input : \n");
       scanf(" %c", &a);
    
    if(isdigit(a))
       {
           if(isalpha(a))
           {
               if(isupper(a))
               {
                   printf("valid");
               }
           }
       }
       else
       {
           printf("invalid");
       };
    when I enter any input that contains all of the elements (uppercase letter, lowercase letter and number), I want my program to print out "valid" as you may see in my code.
    but in the IF function, it worked as I planned only until I put "isalpha" and " isupper" together.

    "isdigit" does not cooperate with other elements.

    how can I solve this problem?

  2. #2
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    This is because you are using your if statements in a nested manner. You need to un nest them that way all of the if statements will be executed instead of relying on the isdigit to be true.
    Code:
    int get_random_number(void)
    {
       return 4; //chosen by fair dice roll.
                 //guaranteed to be random
    }

  3. #3
    Ticked and off
    Join Date
    Oct 2011
    Location
    La-la land
    Posts
    1,728
    Quote Originally Posted by Yoonjong Kook View Post
    this is what I am trying to do
    Your code reads, and considers, only one character. It tests whether that character is a digit, and a letter, and an uppercase letter. (Which is basically impossible, there is no character that is all three at the same time.)

    Instead, consider reading the input into a buffer, for example using fgets(), and using a loop to check each character in it. In your lectures, you should have been told that you can define a character array as
    Code:
       char line[250];
    and if you put a string in it, the maximum number of characters is one less, because the end-of-string mark (nul byte, \0) takes one. To read a line into such a buffer, use
    Code:
        if (fgets(line, sizeof line, stdin) == NULL) {
            /* End of input! You should tell the user, then exit.
             * The line array does not have anything new,
             * and we should not examine it!
            */
        }
    Hopefully, your lecturer has also told you in the course notes that if you want to remove the newline at the end of the line -- the newline character (usually \n) is considered part of the line --, include <string.h> and you can use
    Code:
        line[strcspn(line, "\n")] = '\0';
    which techinally removes everything starting at the first occurrence of a newline. Sometimes I use line[strcspn(line, "\r\n")] = '\0'; which removes everything starting at the first carriage return or newline, whichever occurs first. If the string in the array does not contain any of those characters, this does nothing (and is still safe to do).

    Your rules can be reworded as "If there is at least one digit, and if there is at least one letter, and at least one uppercase letter, then the string is valid."

    So, I suggest you set up counter variables (integers -- I'd use unsigned integers, because these counters can never be negative), and count the number of characters that are digits, the number of characters that are letters, and the number of characters that are uppercase letters.

    Note that these rules are not exclusive, as the character classes may overlap: a character can be a letter, and it can be an uppercase letter. So, I would allow the sum of those counters to exceed the number of characters in the string -- check each letter for the conditions separately, one after another, but in one loop.

    After checking each character in the string, base your decision on the counters.

    As an added step, you could consider how you would add a new character class, say spaces and tabs (isblank()), and reject strings that have spaces or tabs in them. Or, say, more than one space or tab.

    Instead of a line buffer, you could also read the input character by character, using getchar() or getc(stdin) or fgetc(stdin), and examine each character separately, but using counters like I outlined above. The difference in this case is that you don't have a char array, and instead of end of string mark, your loop stops when it encounters either newline (\0) or end-of-file mark (EOF).

    I am having problems with isdigit, isalpha, isupper.-random_number-png
    Last edited by Nominal Animal; 10-28-2015 at 01:57 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. input validation using isalpha and isdigit?
    By jpanther in forum C++ Programming
    Replies: 3
    Last Post: 07-05-2015, 01:27 PM
  2. how to use isalpha and isdigit?
    By Wei Yoong in forum C Programming
    Replies: 3
    Last Post: 10-27-2012, 12:41 PM
  3. isalpha
    By EssiJoon in forum C Programming
    Replies: 9
    Last Post: 04-19-2012, 03:18 PM
  4. Using isalpha()
    By Cmuppet in forum C Programming
    Replies: 4
    Last Post: 08-04-2004, 01:12 PM
  5. Help w/isupper function
    By Nicholas35 in forum C++ Programming
    Replies: 4
    Last Post: 01-28-2002, 06:54 AM