codescracker
c

C Program to Delete Vowels from String



« Previous Program Next Program »

In this article, you will learn and get code about how to delete or remove all the vowels from a given string. For example, if the given string is:

codescracker.com

As you all knows that there are 5 vowels from all 26 alphabet characters. That are either A, E, I, O, U (from uppercase alphabet, A-Z) or a, e, i, o, u (from lowercase alphabet, a-z). Then after deleting all the vowels from given string, the string will become:

cdscrckr.cm

Now let's move on to the program.

Delete Vowels from String in C

To delete vowels from the string in C programming, you have to ask from user to enter the string. Now check for vowel (a, A, e, E, i, I, o, O, u, U). If any one gets found (of the 10) then move the next character to its one index back, until the last and so on.

The question is, write a program in C that removes vowels from given string by user at run-time. The answer to this question is:

// Delete Vowels from String in C
// ----codescracker.com-----

#include<stdio.h>
#include<conio.h>
int main()
{
    char str[50];
    int i=0, j, chk;
    printf("Enter a String: ");
    gets(str);
    while(str[i]!='\0')
    {
        chk=0;
        if(str[i]=='a'||str[i]=='e'||str[i]=='i'||str[i]=='o'||str[i]=='u'||str[i]=='A'||str[i]=='E'||str[i]=='I'||str[i]=='O'||str[i]=='U')
        {
            j=i;
            while(str[j-1]!='\0')
            {
                str[j] = str[j+1];
                j++;
            }
            chk = 1;
        }
        if(chk==0)
            i++;
    }
    printf("\nString (without vowels): %s", str);
    getch();
    return 0;
}

You can also write the condition of if block spread into multiple lines as shown below:

if(str[i]=='a' || str[i]=='e' || str[i]=='i' ||
   str[i]=='o' || str[i]=='u' || str[i]=='A' ||
   str[i]=='E' || str[i]=='I' || str[i]=='O' ||
   str[i]=='U')
{
    j=i;
    while(str[j-1]!='\0')
    {
        str[j] = str[j+1];
        j++;
    }
    chk = 1;
}

This program was build and run under Code::Blocks IDE. Here is it sample run:

c program delete vowels from string

Now supply any string say codescracker.com and press ENTER key to see the following output:

remove vowels from string c

Let's take another sample run in which the given string has more than one vowels present at adjacent index.

delete vowels from string program c

As you can see from the string,

welcome to ccooddeessccrraacckkeerr.ccoomm

have many times, vowels preset at adjacent index. So the dry run using this input goes like:

  1. Variable str holds the string welcome to ccooddeessccrraacckkeerr.ccoomm, in a way that
    • str[0] holds w
    • str[1] holds e
    • str[2] holds l
    • and so on
  2. The last character m holds in str[42]
  3. And a null terminated character \0 gets automatically initialized to str[43]
  4. Initially the value of i is 0
  5. At first run, condition of while loop evaluates to be true, because the character present at 0th index (which is w) is not equal to a null terminated character, so program flow goes inside the loop
  6. 0 gets initialized to chk
  7. Because w is not a vowel, so the condition of first if block evaluates to be false
  8. Therefore program flow goes and checks the condition of second if block. There it checks whether the value of chk is 0 or not
  9. Because it is 0, so condition evaluates to be true, and value of i gets incremented
  10. Now program flow goes back to the condition of while loop, and evaluates its condition to be true again, because the character present at 1st index (which is e) is not equal to a null terminated character, so program flow goes inside the loop
  11. Process 6th step
  12. This time, because e is a vowel, so the condition of first if block inside the while loop evaluates to be true
  13. The value of i gets initialized to j
  14. Now shift the all the forward character (from here) to its one index back
  15. That is, if the value of j is 1, then
    • character present at index no.2 gets moved to index no.1
    • similarly character present at index no.3 gets moved to index no.2
    • and so on
    • until a null terminated character gets occurred
  16. After shifting the character, now the current position of all the characters in the string variable str in a way that
    • str[1] holds l
    • str[2] holds c
    • str[3] holds o
    • and so on
  17. From where the shifting gets performed, there only the position of character to index gets replaced.

  18. 1 gets initialized to chk variable
  19. Here initialization process performed to check whether this loop evaluated or not.
  20. If evaluated, then don't increment the value of i
  21. Otherwise increment the value of i
  22. And program flow goes back to the condition of while loop
  23. Because, the value of variable does not gets incremented, so its value will be same as of previous one, that is 1
  24. But this time, the character present at first position is not e, rather it is l
  25. Continue from the step no.7 with l as new character
  26. The process continue running until all the vowels gets deleted from the string. Thats it

using Function

This program uses a user-defined function checkVowel() to check whether the current character is a vowel or not. If it is a vowel, then function returns 1 as its return value, otherwise it will return 0 as its return value. Here we have just placed the function as the condition of if block to check for vowel. Because 1 evaluates to be true condition and 0 evaluates to be a false condition. Then if function returns 1, then condition evaluated to be true, otherwise evaluated to be false. Let's have a look at the program. To learn more about function, then refer to Function in C tutorial.

// Delete Vowels from String in C using Function
// ----codescracker.com-----

#include<stdio.h>
#include<conio.h>
int checkVowel(char);
int main()
{
    char str[50];
    int i=0, j, chk;
    printf("Enter a String: ");
    gets(str);
    while(str[i]!='\0')
    {
        chk=0;
        if(checkVowel(str[i]))
        {
            j=i;
            while(str[j-1]!='\0')
            {
                str[j] = str[j+1];
                j++;
            }
            chk = 1;
        }
        if(chk==0)
            i++;
    }
    printf("\nString (without vowels): %s", str);
    getch();
    return 0;
}
int checkVowel(char ch)
{
    if(ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u' ||
       ch=='A' || ch=='E' || ch=='I' || ch=='O' || ch=='U')
       return 1;
    else
        return 0;

}

This program produces the same output as of previous one.

Same Program in Other Languages


« Previous Program Next Program »