by Dinesh Thakur Category: Control Structures

The program segment given below uses a nested if statement to determine whether a given character is a letter or not. In addition, if the given character is a letter, it tests whether it is a vowel or consonant.

ch= getchar();

   if (ch >= 'A' && ch <= 'Z' || ch >='a' && ch <= 'z' )

     {

          /* ch is a letter, test for vowel */

         if (ch == 'A' || ch == 'E' || ch == 'I' || ch== 'O' || ch== 'U' || ch == 'a' || ch == 'e' || ch == 'i' || ch== 'o' || ch== 'u' )

             printf("%c is vowel\n", ch);   

                else printf("%c is consonant\n", ch);

      }

                else printf("%c is not a letter\n", ch);

The outer if statement first tests whether the given character ch is a letter or not. If ch is a letter (either uppercase or lowercase),the condition

if (ch >= 'A' && ch <= 'Z' || ch >='a' && ch <= 'z' )

evaluates as true and inner if-else statement determines and prints whether ch is a vowel or aconsonant; otherwise, the else block prints that the given character is not a letter.

#include <stdio.h>
void main()
{
char ch;

clrscr();
printf("Enter a Character :\n");
ch= getchar();
if (ch >= 'A' && ch <= 'Z' || ch >='a' && ch <= 'z' )
{
/* ch is a letter, test for vowel */
if (ch == 'A' || ch == 'E' || ch == 'I' || ch== 'O' || ch== 'U' || ch == 'a' || ch == 'e' || ch == 'i' || ch== 'o' || ch== 'u' )
printf("%c is vowel\n", ch);
else printf("%c is consonant\n", ch);
}
else printf("%c is not a letter\n", ch);
getch();
}

      C Program Character test letter, vowel, consonant



About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.



Related Articles