In this tutorial you will learn about the C Program to replace all Vowels in String with given character and its application with practical example.
C Program to replace all Vowels in String with a given character
In this tutorial, we will learn to create a C program that will replace all Vowels in String with a given character in C programming.
Prerequisites
Before starting with this tutorial we assume that you are best aware of the following C programming topics:
- Operators in C Programming.
- Basic Input and Output function in C Programming.
- Basic C programming.
- For loop in c programming.
- String functions of c programming.
- Conditional Statements in the C programming
Replace all Vowels in String with a given character
As we all know the String is a collection of character data types. In strings, only one variable is declared which can store multiple values. First, we will take the input string from the user. Then will take the replacing character from the user and swap it with the vowels in the string.
With the help of this program, we can replace all Vowels in String with a given character.
Algorithm:-
1 2 3 4 5 6 7 8 9 10 11 |
1. Declare the variables for the program. 2. Take the input string from the user. 3. Take the replacing character from the user. 4. Replacing the string. 5. Print the string. 6. End the program. |
Program to replace all Vowels in String with a given character:-
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 |
#include<stdio.h> #include<string.h> int main() { //declaring the variables for the program char str[50], ch, i; //taking input string form the user printf("Enter any string: "); gets(str); //taking the replacing character from the user printf("Enter any character: "); scanf("%c", &ch); //Replacing the vovewls from the characters for(i=0; str[i]!='\0'; i++) { 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') { str[i] = ch; } } //Printing the output string from to the user after replacing the character printf("\nNew String (after replacing vowel with %c) = %s", ch, str); return 0; } |
Output:-
In the above program, we have first initialized the required variable.
- str[50] = it will hold the string value.
- ch = it will hold the string value.
- i = it will hold the string value.
Input strings from the user.
Replacing the vowel with the character.
Printing the output.