This blog is under construction

Monday 15 July 2013

C program to delete all consonants from a string

Write a C program to delete all consonants from a sentence.


  #include <stdio.h>
  #include <string.h>

  int main() {
        int i = 0, j = 0, k;
        char string[256], result[256];

        /* vowels in lowercase and uppercase */
        char vowel[10] = {'a', 'e', 'i', 'o', 'u',
                                  'A', 'E', 'I', 'O', 'U'};

        /* get the input string from the user */
        printf("Enter your input string:");
        fgets(string, 256, stdin);
        string[strlen(string) - 1] = '\0';

        /* copy consonants alone to result array */
        while (string[i] != '\0') {
                for (k = 0; k < 10; k++) {
                        if (vowel[k] == string[i]) {
                                result[j++] = string[i];
                                break;
                        }
                }

                i++;
        }

        result[j] = '\0';

        /* copy the contents of result to string */
        strcpy(string, result);

        /* print the result */
        printf("After deleting all consonants: %s\n", string);
        return 0;
  }



  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  Enter your input string: ExpenditureAndAllowance
  After deleting all consonants: EeiueAAoae


4 comments:

  1. But the special characters are also being deleted... What if I want only to remove the consonants from a string consisting of special characters and alphabets both??

    ReplyDelete