Tutorialspoint
Problem
Solution
Submissions

Replace All Vowels in a String

Certification: Basic Level Accuracy: 87.5% Submissions: 8 Points: 10

Write a C++ program that replaces all vowels in a given string with a specific character.

Example 1
  • Input: string = "Hello World", replacement = '*'
  • Output: "H*ll* W*rld"
  • Explanation:
    • Step 1: Identify all vowels (a, e, i, o, u) in the input string.
    • Step 2: Replace each vowel with the replacement character '*'.
    • Step 3: Return the modified string.
Example 2
  • Input: string = "C++ is fun", replacement = '#'
  • Output: "C++ #s f#n"
  • Explanation:
    • Step 1: Identify all vowels (a, e, i, o, u) in the input string.
    • Step 2: Replace each vowel with the replacement character '#'.
    • Step 3: Return the modified string.
Constraints
  • 1 ≤ len(string) ≤ 10^6
  • String contains printable ASCII characters
  • Time Complexity: O(n), where n is the length of the string
  • Space Complexity: O(n)
ArraysStringsCapgeminiApple
Editorial

Login to view the detailed solution and explanation for this problem.

My Submissions
All Solutions
Lang Status Date Code
You do not have any submissions for this problem.
User Lang Status Date Code
No submissions found.

Please Login to continue
Solve Problems

 
 
 
Output Window

Don't have an account? Register

Solution Hints

  • Iterate through the string and replace each vowel with the specified character.
  • Use a set or string of vowels to check if a character is a vowel.
  • Handle both lowercase and uppercase vowels.

Steps to solve by this approach:

 Step 1: Define a function replaceVowels that takes a constant string reference and a character.
 Step 2: Create a string containing all vowels for checking.
 Step 3: Make a copy of the input string to modify.
 Step 4: Iterate through each character in the result string.
 Step 5: Check if the current character is a vowel using string::find.
 Step 6: If it's a vowel, replace it with the specified character.
 Step 7: Return the modified string.

Submitted Code :