This blog is under construction

Thursday 11 July 2013

C program to check whether a character is vowel or consonant

Write a C program to check whether a character is vowel or consonant.


  #include <stdio.h>

  int main() {
        int flag = 0, i;
        char ch, vowel[] = {'a', 'e', 'i', 'o', 'u',
                                     'A', 'E', 'I', 'O', 'U'};

        /* get the input character from the user */
        printf("Enter your input character:");
        ch = getchar();

        /* check whether input is vowel or consonant */
        if ((ch >= 'A' && ch <= 'Z') ||
                (ch >= 'a' && ch <= 'z')) {
                for (i = 0; i < 10; i++) {
                        if (vowel[i] == ch) {
                                flag = 1;
                                printf("%c is a vowel!!\n", ch);
                                break;
                        }
                }

                if (!flag) {
                        printf("%c is a consonant!!\n", ch);
                }

        } else {
                printf("%c is not an alphabet!!\n", ch);
        }

        return 0;
  }



  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  Enter your input character:A
  A is a vowel!!

  jp@jp-VirtualBox:~/$ ./a.out
  Enter your input character:1
  1 is not an alphabet!!

  jp@jp-VirtualBox:~/$ ./a.out
  Enter your input character:a
  a is a vowel!!

  jp@jp-VirtualBox:~/$ ./a.out
  Enter your input character:Z
  Z is a consonant!!



No comments:

Post a Comment