Pages

Monday, November 23, 2009

Character Functions

There are many functions in C which can help the user in processing character variables. The string functions described in the previous session will work only with strings (character arrays) therfore we need some functions which can work on characters also. All these functions are defined in the header file so it is mandatory to include this file before using any of the following functions.
1. isalpha – returns true if the argument is an alphabet, returns false otherwise.
Syntax: void isalpha(char)
Example 1 : Checking if the user has entered an alphabet or not.

#include
void main()
{
char c;
printf(“\nEnter an alphabet : “);
scanf(“%c”,&c);
if(isalpha(c)) // check if the entered value is a alphabet
printf(“\nThis is an alphabet”);
else
printf(“\nThis is either a digit or a symbol”);
}

2. isalpha – returns true if the argument is a digit, returns false otherwise.
Syntax : void isdigit(char)
Example 2 : Checking if the user has entered a digit or not.

#include
void main()
{
char c;
printf(“\nEnter a digit : “);
scanf(“%c”,&c);
if(isdigit(c)) // check if the entered value is a digit
{
printf(“\nThis is a digit”);
else
printf(“\nNot a digit”);
}
3. isalnum – returns true if the argument is an alphabet or a digit, returns false otherwise.
Syntax : void isalnum(char)
Example 3: Checking if the user has entered a digit or not.

#include
void main()
{
char c;
printf(“\nEnter a digit or an alphabet : “);
scanf(“%c”,&c);
if(isalnum(c))
printf(“\nThis is either an alphabet or a digit”);
else
printf(“\nYou’ve entered a symbol”);
}

4. isupper – returns true if the argument is a capital letter, returns false otherwise.
Syntax: void isupper (char)
Example 4: Checking if the user has entered a capital case alphabet or not.

#include
void main()
{
char c;
printf(“\nEnter an alphabet : “);
scanf(“%c”,&c);
If(isalpha(c))
{
if(isupper(c))
printf(“\nThis is a capital letter”);
else
printf(“\nThis is a small letter”);
}
else
printf(“\nThis is not an alphabet”);
5. islower – returns true if the argument is a small letter, returns false otherwise.
Syntax: void islower(char)
Example 5 : Checking if the user has entered a small case alphabet or not.

#include
void main()
{
char c;
printf(“\nEnter an alphabet : “);
scanf(“%c”,&c);
if(isalpha(c))
{
if(islower(c))
printf(“\nThis is a small letter”);
else
printf(“\nThis is a capital letter”);
}
else
printf(“\nThis is not an alphabet”);
}
6. isspace – returns true if the argument is a whitespace (space), returns false otherwise.
Syntax : void isspace(char)
Example 2 : Checking if the user has entered a space or not.
#include
void main()
{
char c;
printf(“\nEnter any character : “);
scanf(“%c”,&c);
if(isspace(c))
printf(“\nSpace not allowed”);
else
printf(“\nYou entered %c”,c);
}

0 comments:

Post a Comment