Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Isalpha Function Issue.

Isalpha Function Issue.

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorialquestion
7 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mike Certini
    wrote on last edited by
    #1

    In the below code, I am attempting to count the number of letters and number of words in a phrase. In researching the return values of isalpha(), I noted that when a letter is present the first character isalpha() output is 1. But the second character isalpha() output is 2? The phrase that was input into this example was the following: How do you organize? Can someone help me with why I am not getting uniform output for the "H" character and the "o" character?

    #include <stdio.h>
    #include <ctype.h>

    int main(void)
    {
    int ch;
    float avg = 0;
    int spaces = 0;
    int letters = 0;
    int test = 0;
    int ltrcnt = 0;
    int spccnt = 0;
    while((ch = getchar()) != '\n')
    {
    test = isalpha(ch);
    if(isalpha(ch) == 2)
    {
    ltrcnt++;
    }
    if(isspace(ch) == 8)
    {
    spccnt++;
    }
    }
    printf("The Number Of Words Are %d , The Number Of Letters Are %d ",(spccnt+1),ltrcnt);

    }
    
    C L 2 Replies Last reply
    0
    • M Mike Certini

      In the below code, I am attempting to count the number of letters and number of words in a phrase. In researching the return values of isalpha(), I noted that when a letter is present the first character isalpha() output is 1. But the second character isalpha() output is 2? The phrase that was input into this example was the following: How do you organize? Can someone help me with why I am not getting uniform output for the "H" character and the "o" character?

      #include <stdio.h>
      #include <ctype.h>

      int main(void)
      {
      int ch;
      float avg = 0;
      int spaces = 0;
      int letters = 0;
      int test = 0;
      int ltrcnt = 0;
      int spccnt = 0;
      while((ch = getchar()) != '\n')
      {
      test = isalpha(ch);
      if(isalpha(ch) == 2)
      {
      ltrcnt++;
      }
      if(isspace(ch) == 8)
      {
      spccnt++;
      }
      }
      printf("The Number Of Words Are %d , The Number Of Letters Are %d ",(spccnt+1),ltrcnt);

      }
      
      C Offline
      C Offline
      Cool_Dev
      wrote on last edited by
      #2

      isalpha returns zero if the given character is not an alphabet. Returns non-zero if it is alphabet. And may return '1' if the alphabet is of upper case, and '2' if it is lower case.

      A M 2 Replies Last reply
      0
      • C Cool_Dev

        isalpha returns zero if the given character is not an alphabet. Returns non-zero if it is alphabet. And may return '1' if the alphabet is of upper case, and '2' if it is lower case.

        A Offline
        A Offline
        Andrew Brock
        wrote on last edited by
        #3

        Yes, note the following defines from ctype.h

        #define _UPPER 0x1 /* upper case letter */
        #define _LOWER 0x2 /* lower case letter */
        #define _DIGIT 0x4 /* digit[0-9] */
        #define _SPACE 0x8 /* tab, carriage return, newline, */
        /* vertical tab or form feed */
        #define _PUNCT 0x10 /* punctuation character */
        #define _CONTROL 0x20 /* control character */
        #define _BLANK 0x40 /* space char */
        #define _HEX 0x80 /* hexadecimal digit */

        #define _LEADBYTE 0x8000 /* multibyte leadbyte */
        #define _ALPHA (0x0100|_UPPER|_LOWER) /* alphabetic character */

        M 1 Reply Last reply
        0
        • A Andrew Brock

          Yes, note the following defines from ctype.h

          #define _UPPER 0x1 /* upper case letter */
          #define _LOWER 0x2 /* lower case letter */
          #define _DIGIT 0x4 /* digit[0-9] */
          #define _SPACE 0x8 /* tab, carriage return, newline, */
          /* vertical tab or form feed */
          #define _PUNCT 0x10 /* punctuation character */
          #define _CONTROL 0x20 /* control character */
          #define _BLANK 0x40 /* space char */
          #define _HEX 0x80 /* hexadecimal digit */

          #define _LEADBYTE 0x8000 /* multibyte leadbyte */
          #define _ALPHA (0x0100|_UPPER|_LOWER) /* alphabetic character */

          M Offline
          M Offline
          Mike Certini
          wrote on last edited by
          #4

          Andrew, Ahh, Thank you for directing me to the defines. Since I am an newbie, I have never looked in the header file. Thank you for the answer as well.

          L 1 Reply Last reply
          0
          • C Cool_Dev

            isalpha returns zero if the given character is not an alphabet. Returns non-zero if it is alphabet. And may return '1' if the alphabet is of upper case, and '2' if it is lower case.

            M Offline
            M Offline
            Mike Certini
            wrote on last edited by
            #5

            Cool_Dev, Thank you for the answer.

            1 Reply Last reply
            0
            • M Mike Certini

              In the below code, I am attempting to count the number of letters and number of words in a phrase. In researching the return values of isalpha(), I noted that when a letter is present the first character isalpha() output is 1. But the second character isalpha() output is 2? The phrase that was input into this example was the following: How do you organize? Can someone help me with why I am not getting uniform output for the "H" character and the "o" character?

              #include <stdio.h>
              #include <ctype.h>

              int main(void)
              {
              int ch;
              float avg = 0;
              int spaces = 0;
              int letters = 0;
              int test = 0;
              int ltrcnt = 0;
              int spccnt = 0;
              while((ch = getchar()) != '\n')
              {
              test = isalpha(ch);
              if(isalpha(ch) == 2)
              {
              ltrcnt++;
              }
              if(isspace(ch) == 8)
              {
              spccnt++;
              }
              }
              printf("The Number Of Words Are %d , The Number Of Letters Are %d ",(spccnt+1),ltrcnt);

              }
              
              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              You should familiarise yourself with the MSDN pages[^]; they contain a wealth of useful information.

              I must get a clever new signature for 2011.

              1 Reply Last reply
              0
              • M Mike Certini

                Andrew, Ahh, Thank you for directing me to the defines. Since I am an newbie, I have never looked in the header file. Thank you for the answer as well.

                L Offline
                L Offline
                Luc Pattyn
                wrote on last edited by
                #7

                And you don't have to look there, what matters is the documentation: isalpha return a boolean result, either zero or non-zero; since the different non-zero values are not documented, you should not rely on them. Nor on any observation you may make, not in a .h file and not when running a test app. :)

                Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

                Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups