Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Ctype and Functions

Status
Not open for further replies.

Sirrach

Programmer
Apr 2, 2002
1
US
How would you get a character input from user and then use that input to test it in one of the character handling libraries? Thanks for any help.
 
Here is code which checks if the character entered by a user is an alphabetic character:

Code:
#include <ctype.h>    /* isalpha() and friends */
#include <stdio.h>    /* printf() */

 ...
     int ch;

      ...
      ch = getchar();
      if (isalpha(ch)) {
          printf (&quot;Alphabetic!\n&quot;);
      } else {
          printf (&quot;Non-alphabetic\n&quot;);
      }
      ...
The function isalpha() and other character classification functions from ctype.h return a non-zero value if the condition being tested is true, and zero otherwise.
 
You'll also want to cast the argument passed to any of the to* and is* functions in <ctype.h> to unsigned char.

Even though they all accept an int, the C standard requires that the argument be in the range of 0 to UCHAR_MAX or EOF for the behavior to be defined.
Russ
bobbitts@hotmail.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top