C Program Remove vowels from a string – If a problem statement is given, the first step is to understand it completely and also see if there are any constraints mentioned. Then, we decide on the inputs required to solve this along with the output to expect at the end. Later, we determine the logic to be used to get this desired output by using the inputs taken.
The given problem statement is, to remove all the vowels of the input string and display it without any vowels.
For this, we require a string as input whose vowels are to be removed. The output we expect in the end is also a string which doesn’t contain any vowel at all.
To get the input string (c) at runtime, we can use gets() function. This is a predefined function that reads one line string input and stores it in a string pointer or character array.
This continues to read until until in encounters either a newline or end-of-file. The syntax for this is – gets(string *s).
char c[200];
printf(“enter the string:”);
gets(c);
To store the string without any vowels, we will require another string (c1). We traverse the input string from 0th index to the end (c[i]=’\0′) and check whether each character is a vowel (a, e, i, o, u). If yes then, we do not do anything.
Else, if it is not a vowel then, we store that character in the other string (c1) and increment the index (initially zero) by 1.
for (i=0,j=0;c[i]!=’\0′;i++) {
if(c[i]==’a’ || c[i]==’e’ || c[i]==’i’ || c[i]==’o’ || c[i]==’u’);
else {
c1[j++]=c[i];
}
}
When we exit the loop after completion of all iterations, the c1 character array (string) will now have all the characters of the input string (c) without any vowels included.
This c1 array is nothing but, our desired resultant output string which can be displayed on the console screen using printf().
printf(“string after removals of vowels: %s”,c1);
Delete Vowels from A String in C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#include<stdio.h> #include<conio.h> int main () { char c[200],c1[200]; int t=0,i,j; printf("enter the string:"); gets(c); for (i=0,j=0;c[i]!='\0';i++) { if(c[i]=='a' || c[i]=='e' || c[i]=='i' || c[i]=='o' || c[i]=='u'); else { c1[j++]=c[i]; } } printf("string after removals of vowels: %s",c1); } |
Output:
1 2 |
enter the string:hello world string after removals of vowels: hll wrld |
Remove Vowels from A String Using Function
In the above method, the entire code was within main method only. The disadvantage due to this is that, no part of the code would be reusable and the code itself is not very readable.
To get over these disadvantages, we can use separate functions.
If we split the code based on the logic used and place it in different function block then, if we require the same set of statements or logic elsewhere, we can just make a function call instead of rewriting it thereby, making the code reusable.
Apart from that, if we place it in separate function blocks then, the code it more readable and looks very clean.
If we need to find some part of the code then instead of going through entire main method, we can directly go to its respective function block.
In our given problem statement, we split it into two. The main method handles all the i/o operations while the user-defined function (Removevowels) handles the logic behind the solution.
Main method first reads the input string using gets() function at runtime.
Then, it makes a function call to Removevowels (user-defined function) by passing the input string (c) as argument.
This user-defined function (Removevowels) has the same logic as that of above but, with a slight difference that, instead of using another string (c1) we store the modified value in the same input string or argument(c).
After completing the entire logic present in this user-defined function (Removevowels), the string (c) contains our desired output which is displayed on the output console screen in the main method by using the printf() function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
#include<stdio.h> #include<conio.h> int Removevowels(char *c) { int i,j; for (i=0,j=0;c[i]!='\0';i++) { if(c[i]=='a' || c[i]=='e' || c[i]=='i' || c[i]=='o' || c[i]=='u'); else { c[j++]=c[i]; } } c[j]='\0'; } int main () { char c[200]; printf("enter the string:"); gets(c); Removevowels(c); printf("string after removals of vowels: %s",c); } |