Program to duplicate Vowels in String

Given a string “str”, the task is to duplicate vowels in this string.

Examples:

Input: str = "geeks"
Output: geeeeks

Input: str = "java"
Output: jaavaa

Approach: Iterate the string using a loop. Check if the character is a vowel and duplicate it. Return
then print the resultant string.



Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program for printing string
// with duplicate vowels
  
#include <bits/stdc++.h>
using namespace std;
  
// Function to check for the Vowel
bool isVowel(char ch)
{
    ch = toupper(ch);
    return (ch == 'A' || ch == 'E'
            || ch == 'I' || ch == 'O'
            || ch == 'U');
}
  
// Function to get the resultant string
// with vowels duplicated
string duplicateVowels(string str)
{
    int t = str.length();
  
    // Another string to store
    // the resultant string
    string res = "";
  
    // Loop to check for each character
    for (int i = 0; i < t; i++) {
        if (isVowel(str[i])) {
            res += str[i];
        }
        res += str[i];
    }
  
    return res;
}
  
// Driver Code
int main()
{
    string str = "helloworld";
  
    // Print the original string
    cout << "Original String: "
         << str << endl;
  
    string res = duplicateVowels(str);
  
    // Print the resultant string
    cout << "String with Vowels duplicated: "
         << res << endl;
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program for printing String
# with duplicate vowels
  
  
# Function to check for the Vowel
def isVowel(ch):
    ch = ch.upper()
    if (ch == 'A' or ch == 'E' or 
        ch == 'I' or ch == 'O' or 
        ch == 'U'):
        return True
    else:
        return False
  
# Function to get the resultant String
# with vowels duplicated
def duplicateVowels(S):
    t = len(S)
  
    # Another to store
    # the resultant String
    res = ""
  
    # Loop to check for each character
    for i in range(t):
        if (isVowel(S[i])):
            res += S[i]
        res += S[i]
  
    return res
  
# Driver Code
S = "helloworld"
  
# Print the original String
print("Original String: ", S)
  
res = duplicateVowels(S)
  
# Print the resultant String
print("String with Vowels duplicated: ", res)
  
# This code is contributed by Mohit Kumar

chevron_right


Output:

Original String: helloworld
String with Vowels duplicated: heelloowoorld


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : mohit kumar 29



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.