Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

C Program to Remove Vowels from a String

Last Updated on May 15, 2023 by Prepbytes

Removing vowels from a string means deleting all the vowels (i.e., the letters ‘a’, ‘e’, ‘i’, ‘o’, and ‘u’, both in uppercase and lowercase) from a given string. This can be done by iterating through each character of the string and checking if it is a vowel or not. If it is, then we can delete that character from the string by shifting all the characters to the left by one position. We repeat this process for each vowel in the string until all the vowels have been removed. Finally, we print the modified string without the vowels.

How to Remove Vowels from a String

Below are some approaches to remove vowels from a string:

Naive Approach: Using Loops

The goal is to repeatedly examine each character in a string to determine whether or not it is a vowel. Delete the character from the string if it is a vowel, then continue on to the following element.

Algorithm
The steps are as follows:

  • Step 1: Make the variables ready.
  • Step 2: Accept the data
  • Step 3: Create the loop. Verify the vowels and eliminate them.
  • Step 4: Store the string empty of vowels using another for a loop.
  • Step 5: Ending both loops is necessary. Take the string, remove any vowels, and print it.

Code Implementation

#include <stdio.h>
#include <stdlib.h>
#include<string.h>
void solve()
{
    
    char c[100];
    int i, j, length = 0;


    
    printf("Enter a string : ");
    printf("\n");
    // gets(c);
    scanf("%s", c);

    length = strlen(c);

    for (i = 0; i < length; i++) {
       
        if (c[i] == 'A' || c[i] == 'E' || c[i] == 'I' || c[i] == 'O' || c[i] == 'U' ||
         c[i] == 'a' || c[i] == 'e' || c[i] == 'i' || c[i] == 'o' || c[i] == 'u' ) {
           
            for (j = i; j < length; j++) {
              
                c[j] = c[j + 1];
            }
            i--;
            length--;
        }
        c[length + 1] = '\0';
    }
   printf("After Deletion the string will be  : %s", c);
 
}

int main()
{
    solve();
    return 0;
}

Output

Enter a string: Prepbytes
After Deletion, the string will be: Prpbyts

Time Complexity: O(N^2) where n is the length of the string.

Space Complexity: O(1)

Pointer Approach: Use the Pointer to Remove Vowels from the String

In this approach, we will use the pointers to remove the vowels from the string.

  • Step 1: Declare a character pointer variable str and a character pointer variable p.
  • Step 2: Allocate memory for str using the malloc() function to hold the input string.
  • Step 3: Prompt the user to enter a string and store it in str using the fgets() function.
  • Step 4: Set p to the beginning of str.
  • Step 5: Loop through each character in the string using a while loop, checking if the current character is a vowel.
  • Step 6: If the current character is a vowel, use a while loop to shift all the characters to the left by one position, effectively removing the vowel from the string.
  • Step 7: If the current character is not a vowel, increment the pointer p to the next character in the string.
  • Step 8: Repeat steps 5-7 until the end of the string is reached.
  • Step 9: Print the modified string without the vowels using the printf() function.
  • Step 10: Free the memory allocated for str using the free() function.

Code Implementation

#include <stdio.h>
#include <stdlib.h>
#include<string.h>
void solve()
{
    
    char c[100];
    int i, j, length = 0;


    
    printf("Enter a string : ");
    printf("\n");
    // gets(c);
    scanf("%s", c);

    length = strlen(c);

    for (i = 0; i < length; i++) {
       
        if (c[i] == 'A' || c[i] == 'E' || c[i] == 'I' || c[i] == 'O' || c[i] == 'U' ||
         c[i] == 'a' || c[i] == 'e' || c[i] == 'i' || c[i] == 'o' || c[i] == 'u' ) {
           
            for (j = i; j < length; j++) {
              
                c[j] = c[j + 1];
            }
            i--;
            length--;
        }
        c[length + 1] = '\0';
    }
   printf("After Deletion the string will be  : %s", c);
 
}

int main()
{
    solve();
    return 0;
}

Output

Enter a string: Prepbytes
After Deletion, the string will be: Prpbyts

Conclusion
In conclusion, we have discussed how to write a C program to remove vowels from a given string. We have presented two different approaches to solve this problem: one using loops and array indexing and the other using pointers. Both approaches involve iterating through each character in the string and checking if it is a vowel. If it is, we remove the vowel from the string by shifting all the characters to the left by one position.

Frequently Asked Questions

Q1. What is the purpose of removing vowels from a string?
Ans. There are various reasons why someone might want to remove vowels from a string. For example, it may be part of a larger program that analyzes text data or it may be done to improve the readability or aesthetics of the text.

Q2. Can a string have no vowels?
Ans. Yes, a string can have no vowels. For example, the string "xyz" has no vowels.

Q3. How do I handle uppercase vowels in the string?
Ans. You can handle uppercase vowels by including them in the list of vowels to be removed. In the C program, you can include the uppercase vowels in the vowels array alongside the lowercase vowels.

Q4. Can I modify the original string directly or should I create a new string without vowels?
Ans. Both approaches are valid. You can modify the original string directly, as we have demonstrated in the program using pointers, or you can create a new string without vowels, as we have demonstrated in the program using loops and array indexing.

Q5. How do I handle whitespace and punctuation in the string?
Ans. Whitespace and punctuation are not considered vowels, so they should not be removed from the string. You can include an additional check in your program to skip over whitespace and punctuation when iterating through the characters in the string.

Leave a Reply

Your email address will not be published. Required fields are marked *