Program to Remove Vowels from String in C Language
Program Description:
Write a Program to Remove Vowels from String in C programming language. The program will accept an input string from the user and removes all vowels from the string and returns the results.
We are going to look at different methods to Remove the vowels from a string in C. Each program will be followed by a detailed step-by-step explanation and program output. We also provided instructions for compiling and running the program using your favorite compiler.
📢 This Program is part of the C String Practice Programs series
Before going to write the program. Let’s understand the program requirements by looking at an example input and output of the program.
Program Input and Output:
Input:
Enter a string : Learn C Programming
Output:
String before removing Vowels :Learn C Programming(Actual string)
String after removing Vowels:Lrn C Prgrmmng(resultant string)
If you notice, The output string( Lrn C Prgrmmng) doesn’t contain any vowels. i.e the vowels are a, u, i, o, and u.
Prerequisites:
We are going to use C Strings, C Arrays, and Functions. So please go through the following articles to understand the program better.
- Strings in C Language – Declare, Initialize, and Modify Strings
- Different ways to Read and Print strings in C
- Arrays in C with Example Programs
- Functions in C – How to Create and Use functions in C
We will look at the two different methods to Remove Vowels from a String in C Language.
- Iterative Method
- Using User-defined Functions
Let’s look at each method in detail.
Remove Vowels from String in C Program Explanation:
- Start the program by declaring the input string( inputStr) with the size of SIZE character. The SIZE is a global constant, which holds the max size of inputStr
- Declare two variables i and j. These two variables are used as indices. The i variable is used to iterate through the inputStr and the j variable is used to update the inputStr. We will call the variable i as the read index and the variable j as the write index.
- Take the input string from the user and update the inputStr string using the gets function.
- To Remove Volwels from the string, Traverse through the inputStr and check all characters in the string and only update the character in the modified string if the character is not a vowel.
- Start the For Loop by initializing variables i and j with 0. It will continue looping as long as the current character (inputStr[i]) is not a NULL character (‘\0’).
- At each iteration, Inside the for loop, we check if the present character ( inputStr[i]) is a vowel. If it is not a vowel, it is copied to the next position in the modified string ( inputStr[j]) at the write index j.
- Finally, Increment the values of the variable j by 1 and the value of variable i by 1
- If the current character is a Vowel, Then the character will be skipped and Loop continues to the next iteration.
- Once the For loop is finished, Add the Terminating NULL character to the modified string inputStr at the write index ( j). – i.e inputStr[j] = '\0';
- Finally, Display the resultant string on the console using a printffunction.
Program to Remove Vowels from String in C using Iterative Method:
Here is the program to remove vowels from a string in C programming language
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
/* program to Remove Vowels in a string in C sillycodes.com */ #include<stdio.h> // Max Size of inputString const int SIZE = 100; int main() { // declare a inputString with 'SIZE' char inputStr[SIZE]; int i, j; // get the string from the user printf("Enter a string : "); gets(inputStr); printf("String before removing Vowels :%s(Actual string)\n", inputStr); // Iterate over the string // maintain two indices 'i'-readIndex, 'j'-writeIndex for (i = 0, j = 0; inputStr[i]; ++i) { // Check if the character is a Vowel. // If it is not a Vowel. Copy it to writeIndex('j') position in the modified string. // If it is Vowel, Continue to next step // Check lowercase and Uppercase vowels if (inputStr[i] != 'a' && inputStr[i] != 'e' && inputStr[i] != 'i' && inputStr[i] != 'o' && inputStr[i] != 'u' && inputStr[i] != 'A' && inputStr[i] != 'E' && inputStr[i] != 'I' && inputStr[i] != 'O' && inputStr[i] != 'U') { // present character is not a Vowel. inputStr[j++] = inputStr[i]; } } // Add the Terminating NULL Character at the end of inputStr string. inputStr[j] = '\0'; // Print Results printf("String after removing Vowels:%s(resultant string)\n", inputStr); return 0; } |
Program Output:
Let’s compile and run the program using your favorite compiler. We are using the GCC compiler in this example.
1 2 3 4 5 6 |
$ gcc remove-vowels.c $ ./a.out Enter a string : Learn C Programming String before removing Vowels :Learn C Programming(Actual string) String after removing Vowels:Lrn C Prgrmmng(resultant string) $ |
As we can see from the above output, The program removed all vowels from the input string and returned the result.
Let’s try few more example.
1 2 3 4 5 6 7 8 9 10 |
$ ./a.out Enter a string : www.SillyCodes.com String before removing Vowels :www.SillyCodes.com(Actual string) String after removing Vowels:www.SllyCds.cm(resultant string) $ $ ./a.out Enter a string : AAEEIIOOOUUU String before removing Vowels :AAEEIIOOOUUU(Actual string) String after removing Vowels:(resultant string) $ |
Looking like program is properly removing the vowels from the input string and returning the results.
Remove Vowels from String in C using Functions:
In the above program, We have written a complete program inside the main function. It is not a recommended method, Let’s rewrite the above program to use a function to remove the vowels from the string.
By using the functions we can define a function once and can call it as many times as we want. Functions also increase the readability of the program and make it easier to debug issues.. Here are the benefits using the functions in programming.
Here is the program to remove the vowels from a string using a user-defined function in C language.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
/* program to Remove Vowels in a string in C using functions sillycodes.com */ #include<stdio.h> // Max Size of inputString const int SIZE = 100; /** * @brief - Remove the Vowels from the 'inputStr' string * * @param inputStr - Input String */ void removeVowels(char * inputStr) { int i, j; // Iterate over the string // maintain two indices 'i'-readIndex, 'j'-writeIndex for (i = 0, j = 0; inputStr[i]; ++i) { // Check if the character is a Vowel. // If it is not a Vowel. Copy it to writeIndex('j') position in the modified string. // If it is Vowel, Continue to next step // Check lowercase and Uppercase vowels if (inputStr[i] != 'a' && inputStr[i] != 'e' && inputStr[i] != 'i' && inputStr[i] != 'o' && inputStr[i] != 'u' && inputStr[i] != 'A' && inputStr[i] != 'E' && inputStr[i] != 'I' && inputStr[i] != 'O' && inputStr[i] != 'U') { // present character is not a Vowel. inputStr[j++] = inputStr[i]; } } // Add the Terminating NULL Character at the end of inputStr string. inputStr[j] = '\0'; } int main() { // declare a inputString with 'SIZE' char inputStr[SIZE]; // get the string from the user printf("Enter a string : "); gets(inputStr); printf("String before removing Vowels :%s(Actual string)\n", inputStr); // call the removeVowels function with inputStr removeVowels(inputStr); // Print Results printf("String after removing Vowels:%s(resultant string)\n", inputStr); return 0; } |
We have defined a function named removeVowels() to remove the vowels from string. The prototype of this function is void removeVowels(char * inputStr).
This function takes a string( inputStr) as the formal argument and removes all vowels from the inputStr. As we pass the string by address all the changes made inside the inputStr will be visible to the calling function.
Call the removeVowels() function from the main() function and pass the input string( inputStr) to remove the vowels from the string.
removeVowels(inputStr);
Finally, display the resultant string on the screen.
Program Output:
Compile and Run the program and observe the output.
1 2 3 4 5 6 7 8 9 10 11 |
$ gcc remove-vowels-func.c $ ./a.out Enter a string : Programming is Fun String before removing Vowels :Programming is Fun(Actual string) String after removing Vowels:Prgrmmng s Fn(resultant string) $ $ ./a.out Enter a string : HHHAAAEEEIIIOOO String before removing Vowels :HHHAAAEEEIIIOOO(Actual string) String after removing Vowels:HHH(resultant string) $ |
As we can see from the above program inputs and outputs, The removeVowels() function is properly removing the vowels from the string.
Related String Programs:
- C Tutorials Index
- C Programs Index
- C Program to Check Palindrome String
- C Program to Toggle Case of All Characters in String
- C Program to Remove Leading Whitespaces in a String
- C Program to Remove Trailing Whitespaces in a String
- C Program to Remove Extra Spaces between the Words in String
- C Program to Remove All Whitespaces in a String or Sentence
- C Program to Count Frequencies of each character in a string
- C Program to Find Highest Frequency character in a String
- C Program to Find Lowest Frequency Character in a String