C library function isalpha( )

In this tutorial, you will learn about C library function isalpha() which is a character handling function  used to check whether the character is an alphabet or not.

C library function isalpha( )

We should include ctype.h header to use isalpha( ) function.

# include <ctype.h>

C library function isalpha( ): Function prototype


int isalpha( int ch);

This function checks whether its argument is a letter or not (a-z)/(A-Z).

It returns a true value if the argument is a letter (a-z)/(A-Z) and zero (0) otherwise.

Single argument is passed to isalhpa( ).

Example: Demonstration of isalpha( )

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

int main()
{
   printf("Demonstration of isalpha():\n");
   printf("\n%s%s", isalpha('A') ? "A is a " : "A is not a ", "letter");
   printf("\n%s%s", isalpha('c') ? "c is a " : "c is not a ", "letter");
   printf("\n%s%s", isalpha('!') ? "! is a " : "! is not a ", "letter");
   printf("\n%s%s", isalpha('1') ? "1 is a " : "1 is not a ", "letter");

 return 0;
}

[adsense1]

Output

Demonstration of isalpha():

A is a letter
c is a letter
! is not a letter
1 is not a letter

Explanation

In this program, standard library function isalpha is used to determine whether the argument is a letter or not.

Here, conditional operator ?: is used to evaluate the expression.

This can be simply achieved by if...else statement.

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

int main()
{
   printf("Demonstration of isalpha( ):\n");

   if( isalpha('A') == 0)
      printf("\nA is not a letter");
   else
     printf("\nA is a letter");

   if( isalpha('c') == 0)
     printf("\nc is not a letter");
   else
     printf("\nc is a letter");

   if( isalpha('!') == 0)
     printf("\n! is not a letter");
   else
     printf("\n! is a letter");

   if( isalpha('2') == 0)
     printf("\n1 is not a letter");
   else
     printf("\n1 is a letter");

  return 0;
}

Output

Demonstration of isalpha( ):

A is a letter
c is a letter
! is not a letter
1 is not a letter