Program to Remove Consonants from String in C

Program-to-Remove-Consonants-from-String-in-C

Program Description:

Write a Program to Remove Consonants from String in C programming language. The program should accept a string from the user and removes all consonants from the string and returns the results back to the user.

Other characters should not be altered by the program. If the input string contains spaces, numbers, or special characters, the program will keep them all and just remove the consonants.

📢 This Program is part of the C String Practice Programs series

We are going to look at different methods to Remove the consonants from a string in C Language. 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.

Prerequisites:

It is recommended to know the basics of C-Strings, C-Arrays, and Functions in C to understand the following program better.

Example Input and Output:

Input:

Enter a string : Learn Programming Online

Output:

String before removing consonants :Learn Programming Online(Actual string)

String after removing consonants :ea oai Oie(resultant string)

The input string Learn Programming Online and the program removed all consonants and returned the ea oai Oie.

We will look at the two different methods to Remove Consonants from input string in C Language.

  • Iterative Method
  • Using User-defined Functions

Remove Consonants 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. (‘i’-readIndex, ‘j’-writeIndex)
  3. Take the input string from the user and update the inputStr string using the gets function.
  4. To Remove Consonants from a 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 consonant.
  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 consonant. If it is not a consonant, Then it should be Vowel or digit or special character, etc. So Copy the character 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 Consonant, 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.

Use the following condition to confirm the current character is not a consonant

We are using the isalpha() function from the ctype.h header file.

Program to Remove Consonants from String in C Language:

Let’s convert the above algorithm into the C Program, Which will remove all consonants in the given string.

📢 Don’t forget to include the ctype.h header file.

Program Output:

Let’s compile and run the program using GCC compiler (Any compiler)

Test Case 1: Normal characters

remove-consonants-from-string-in-c-program-output

The Program is properly removing the consonants from the string. For example, if the input string is sillycodes.com, Then the program removed all consonants and returned the string ioe.o

Test Case 2: If the input string contains the numbers and the special characters,

In this case, the input string (Test 1723 STRING ?#$& 1) contains the numbers and the special characters, and program removed the consonants and doesn’t change the special characters and numbers.

Remove Consonants from String using a user-defined function in C:

In the above program, We have written the entire program inside the main function. It is not a recommended method, Let’s rewrite the above program to use a function to remove the consonants 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 of using the functions in programming.

Here is the program to remove the consonants from a string using a user-defined function in C language.

We have defined a function named removeConsonants to remove the consonants from the string.

The Prototype of the removeConsonants() function. – void removeConsonants(char *str). This function takes the input string as the argument and removes all consonants from it.

Call the removeConsonants() function from the main() function and pass the input string( inputStr) to Remove the consonants from the string.

As we are passing the string as the address, All the changes made in the removeConsonats function will be reflected in the main() function.

Program Output:

Compile and Run the Program.

remove-consonants-using-function-program-output

As we can see from the above examples, The removeConsonants() function is properly removing the consonants from the input string.

Related 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...

1 Response

  1. […] C Program to Remove Consonants from a String […]

Leave a Reply