Program to Remove Vowels from String in C Language

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.

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:

  1. 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
  2. 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.
  3. Take the input string from the user and update the inputStr string using the gets function.
  4. 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.
  5. 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.
  6. 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';
  7. 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

Program Output:

Let’s compile and run the program using your favorite compiler. We are using the GCC compiler in this example.

program-to-remove-vowels-from-a-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.

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.

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.

program-to-remove-vowels-from-a-string-using-functions

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:

Venkatesh

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

Leave a Reply