Introduction
In this post, i am going to write a c program to remove vowels from even position in a string. We are going to perform the following steps.
Also Read: C Program to Count the Characters in the String Except Space
- Read string from the console.
- Scan the string from left to right until we get null character i.e. ‘\0’.
- If there is any vowel at the even position of the string, then we will remove it.
- The vowels are ‘a’, ‘e’, ‘i’, ‘o’, ‘u’.
- We don’t have to remove these vowels from all the positions.
Just see the following expected output of the c program to remove vowels from even position in a string.
Expected Output
Enter a String
abecidofu
bcdf
As you can see the above output. I have read one random string from the console. The first position of the string is starting from 0th number. After removing all the vowels from the string, our output is bcdf.
Also Read: C Program to Display a String in Capital Letters
Now, in the next section, you can see the actual program.
C Program to Remove Vowels from Even Position in a String
#include <stdio.h>
#include <stdlib.h>
int main()
{
char str[100],dstr[100];
int i=0,j=0;
printf("Enter a String\n");
gets(str);
while(str[i]!='\0')
{
if(i%2==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'))
{
i++;
continue;
}
}
dstr[j]=str[i];
j++;
i++;
}
printf("%s",dstr);
return 0;
}
Now, we will see another program in c to remove all the vowels from the string. In the above program, we have removed only those vowels which are at the even position. But, the next program is removing all the vowels from a string.
Also Read: Switch Case in C Program to Calculate Area of Circle and Triangle
Program to Remove Vowels From a String in C
#include <stdio.h>
#include <stdlib.h>
int main()
{
char str[100],dstr[100];
int i=0,j=0;
printf("Enter a String\n");
gets(str);
while(str[i]!='\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'))
{
i++;
continue;
}
dstr[j]=str[i];
j++;
i++;
}
printf("%s",dstr);
return 0;
}
As you can see the above program, we have removed all the vowels from the string. See the following output.
Output
Enter a String
Anjuman
njmn
The only difference between these two programs is that in the first program, we have written one if condition in which we are comparing the value of i is even or not. If it is even, only then we removing the vowels. But, in the second program, we are simply checking whether the scanned character from a string is a vowel or not. If it is vowel then simply remove it.
Also Read: Program in C to Replace Capital C with Capital S in a File
How do you find a vowel in a string?
In both the above programs, we are using if statement to find the vowel and that if statement is
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'))
There are 10 conditions written in the if statement in both programs. Here, we are simply checking str[i] is a, e, i, o, u, A, E, I, O or U. We have used 10 conditions with logical OR operator. If any condition becomes true, then this if statement becomes true and the body of if statement will be executed.
How do you remove a vowel from a string?
In the above if statement, we are incrementing the value of i and transferring the control to the beginning of the loop. Why? We don’t need to do anything when we encounter a vowel. Actually, we are copying one string into another string. That is why we have taken here two string variables i.e. str and dstr.
The variable str will contain the actual string entered by the user and the another variable dstr will store the string without vowels. See the following code.
dstr[j]=str[i];
Here, we are copying one string into another.
Also Read: Factorial Program in C Programming
I hope you have understood both the programs. If you have any difficulties regarding these two program, you can contact me any time.
Thank you.