- C Programming Examples
- C Programming Examples
- C Print Hello World
- C Get Input from User
- C Print Integer
- C Add Two Numbers
- C Add Subtract Multiply Divide
- C Add n Numbers
- C Area Perimeter of Square
- C Area Perimeter of Rectangle
- C Area Circum of Circle
- C Fahrenheit to Celsius
- C Celsius to Fahrenheit
- C Inches to Centimeters
- C Kilogram to Gram
- C Reverse a Number
- C Swap Two Numbers
- C Interchange Numbers
- C Print ASCII Value
- C Print Fibonacci Series
- C Check Palindrome or Not
- C Check Armstrong or Not
- C Find Armstrong Numbers
- C Find nCr and nPr
- C Find Profit Loss
- C Sum of their Square
- C First & Last Digit Sum
- C Sum of All Digit
- C Product of All Digit
- C Print Total Digit in Number
- C Check Perfect Number
- C Find Basic Gross Salary
- C Round Number to Integer
- C Print Series upto n Term
- C Find Factors of Number
- C if-else & Loop Programs
- C Check Even or Odd
- C Check Prime or Not
- C Check Alphabet or Not
- C Check Vowel or Not
- C Check Leap Year or Not
- C Is Reverse Equal Original
- C Make Calculator
- C Add Digits of Number
- Count Positive Negative Zero
- C Largest of Two Numbers
- C Largest of Three Numbers
- C Smallest of Two Numbers
- C Smallest of Three Numbers
- C Find Factorial of Number
- C Find LCM & HCF
- C Find LCM of n Numbers
- C Find HCF of n Numbers
- C Find Arithmetic Mean
- C Find Average, Percentage
- C Find Student Grade
- C Print Table of Number
- C Print Prime Numbers
- C Find Discount Purchase
- C Calculate Parcel Charge
- C Calculate Wage of Labor
- C Print Phone Bill
- C Conversion programs
- C Decimal to Binary
- C Decimal to Octal
- C Decimal to Hexadecimal
- C Binary to Decimal
- C Binary to Octal
- C Binary to Hexadecimal
- C Octal to Decimal
- C Octal to Binary
- C Octal to Hexadecimal
- C Hexadecimal to Decimal
- C Hexadecimal to Binary
- C Hexadecimal to Octal
- C Pattern Programs
- C Pattern Printing Programs
- C Print Diamond Pattern
- C Print Floyd's Triangle
- C Print Pascal's Triangle
- C Array Programs
- C 1D Array Programs
- C Linear Search
- C Binary Search
- C Largest Element in Array
- C Smallest Element in Array
- C Second Largest/Smallest
- C Count Even Odd
- C Array Element at Even
- C Array Element at Odd
- C Print Even Array Elements
- C Print Odd Array Elements
- C Sum/Product of Even/Odd
- C Reverse an Array
- C Insert Element in Array
- C Delete Element from Array
- C Merge Two Arrays
- C Bubble Sort
- C Selection Sort
- C Insertion Sort
- C Print Common Elements
- C 2D Array Programs
- C Add Two Matrices
- C Subtract Two Matrices
- C Transpose a Matrix
- C Multiply Two Matrices
- C Sum All Matrix Elements
- C Largest Element in Matrix
- C Print Row Column Total
- C 3D Array Programs
- C String Programs
- C Print String
- C Find Length of String
- C Compare Two String
- C Copy a String
- C Concatenate String
- C Reverse a String
- C Count Vowels Consonants
- C Replace Vowel in String
- C Delete Vowels from String
- C Delete Word from String
- C Frequency of Character
- C Count Word in String
- C Remove Spaces from String
- C Sort a String
- C Sort String in Alphabetical
- C Sort Words in Ascending
- C Sort Words in Descending
- C Uppercase to Lowercase
- C Lowercase to Uppercase
- C Swap Two Strings
- C Check Anagram or Not
- C Check Palindrome String
- C Print Number in Words
- C Print Successive Character
- C Character without Space
- C File Programs
- C Read a File
- C Write Content to File
- C Read & Display File
- C Copy a File
- C Merge Two Files
- C Reverse File
- C Count All Character in File
- C List Files in Directory
- C Encrypt & Decrypt a File
- C Delete a File
- C Misc Programs
- Generate Random Numbers
- C Print Date Time
- C Print Message with Time
- C Get IP Address
- C Print Smiling face
- C Pass Array to Function
- Add Two Numbers using Pointer
- C Address of Variable
- C Shutdown Computer
- C Programming Tutorial
- C Tutorial
C Program to Delete Vowels from a String
In this article, you will learn and get code about how to delete or remove all the vowels from a given string. For example, if the given string is:
fresherearth.com
Each of the 26 alphabetic characters has five vowels, as you are all aware. That are either A, E, I, O, and U (from the uppercase alphabet, A-Z) or a, e, i, o, and u (from the lowercase alphabet, a-z). Then, after deleting all the vowels from the given string, the string will become:
cdscrckr.cm
Now let's move on to the program.
In C, remove vowels from a string
To delete vowels from the string in C programming, you have to ask the user to enter the string. Now check for a vowel (a, A, e, E, i, I, o, O, u, U). If any one gets found (of the 10), then move the next character to its one index back, until the last, and so on.
The question is, "Write a program in C that removes vowels from a given string by the user at run-time." The answer to this question is:
#include<stdio.h> #include<conio.h> int main() { char str[50]; int i=0, j, chk; printf("Enter a String: "); gets(str); while(str[i]!='\0') { chk=0; if(str[i]=='a'||str[i]=='e'||str[i]=='i'||str[i]=='o'||str[i]=='u'||str[i]=='A'||str[i]=='E'||str[i]=='I'||str[i]=='O'||str[i]=='U') { j=i; while(str[j-1]!='\0') { str[j] = str[j+1]; j++; } chk = 1; } if(chk==0) i++; } printf("\nString (without vowels): %s", str); getch(); return 0; }
You can also write the condition "if" block spreads into multiple lines, as shown below:
if(str[i]=='a' || str[i]=='e' || str[i]=='i' || str[i]=='o' || str[i]=='u' || str[i]=='A' || str[i]=='E' || str[i]=='I' || str[i]=='O' || str[i]=='U') { j=i; while(str[j-1]!='\0') { str[j] = str[j+1]; j++; } chk = 1; }
This program was built and runs in the Code::Blocks IDE. Here is the sample run:
Now enter any string, such as fresherearth.com, and press the ENTER key to see the output:
Let's take another sample run in which the given string has more than one vowel present at each adjacent index.
As you can see from the string,
welcome to ccooddeessccrraacckkeerr.ccoomm
have many times had vowels preset at the adjacent index. So the dry run using this input goes like this:
- Variable str stores the string "welcome to ccooddeessccrraacckkeerr.ccoomm," in such a way that it can be parsed.
- str[0] holds w.
- str[1] holds e.
- str[2] holds l.
- and so on.
- The final character in str[42] is m.
- A null-terminated character "\0", on the other hand, is automatically initialized to str[43].
- Initially, the value of i is 0.
- At first run, the condition of the while loop evaluates to true because the character present at the 0th index (which is w) is not equal to a null-terminated character, so program flow goes inside the loop.
- The value 0 is set to chk.
- Because w is not a vowel, the first if condition evaluates to false.
- As a result, program flow checks the condition of the second if block. There, it checks whether the value of chk is 0 or not.
- Because it is 0, the condition evaluates to true, and the value of i gets incremented.
- Now program flow goes back to the condition of the while loop and evaluates its condition to be true again because the character present at the 1st index (which is e) is not equal to a null-terminated character, so program flow goes inside the loop.
- Process, 6th step
- This time, because e is a vowel, the condition of the first if block inside the while loop evaluates to true.
- The value of i gets initialized to j.
- Shift all of the forward characters (from here) to the index one back.
- That is, if the value of j is 1, then
- character present at index no.2 gets moved to index no.1.
- Similarly, the character at index no. 3 is moved to index no. 2.
- and so on.
- until a null terminated character occurs.
- After shifting the character, now the current position of all the characters in the string variable str is such that
- str[1] holds l.
- str[2] holds c.
- str[3] holds o.
- and so on.
- From where the shifting gets performed, only the position of the character on the index gets replaced.
- 1 gets initialized to the chk variable.
- Here the initialization process is performed to check whether this loop is evaluated or not.
- If evaluated, then don't increment the value of i.
- Otherwise, increment the value of i.
- And the program flow returns to the while loop's condition.
- Because the variable's value is not incremented, it will be the same as the previous one, which is 1.
- But this time, the character present in the first position is not e, but rather it is l.
- Continue from step no. 7 with 'l' as a new character.
- The process continues running until all the vowels are deleted from the string.
Using a function, remove vowels from a string
This program uses a user-defined function called checkVowel() to check whether the current character is a vowel or not. If the character is a vowel, the function returns 1; otherwise, it returns 0.
To check for vowels, we simply used the function as the condition of an if block. Because 1 indicates a true condition and 0 indicates a false condition. Then, if function returns 1, then condition is evaluated to be true; otherwise, condition is evaluated to be false. Let's have a look at the program.
#include<stdio.h> #include<conio.h> int checkVowel(char); int main() { char str[50]; int i=0, j, chk; printf("Enter a String: "); gets(str); while(str[i]!='\0') { chk=0; if(checkVowel(str[i])) { j=i; while(str[j-1]!='\0') { str[j] = str[j+1]; j++; } chk = 1; } if(chk==0) i++; } printf("\nString (without vowels): %s", str); getch(); return 0; } int checkVowel(char ch) { if(ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u' || ch=='A' || ch=='E' || ch=='I' || ch=='O' || ch=='U') return 1; else return 0; }
This program produces the same output as the previous one.
The same program in different languages
« Previous Program Next Program »