Program to duplicate Vowels in String - GeeksforGeeks

09 Jun, 2021
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++
Java
Python3
C#
Javascript
// C++ program for printing string// with duplicate vowels #include <bits/stdc++.h>using namespace std; // Function to check for the Vowelbool 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 duplicatedstring 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 Codeint 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;}
// Java program for printing string// with duplicate vowelsimport java.util.*; class GFG{     // Function to check for the Vowel    static boolean isVowel(char ch)    {        ch = Character.toUpperCase(ch);        return (ch == 'A' || ch == 'E' ||                ch == 'I' || ch == 'O' || ch == 'U');    }     // Function to get the resultant string    // with vowels duplicated    static 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.charAt(i)))                res += str.charAt(i);            res += str.charAt(i);        }        return res;    }     // Driver Code    public static void main(String[] args)    {        String str = "helloworld";         // Print the original string        System.out.println("Original String: " + str);        String res = duplicateVowels(str);         // Print the resultant string        System.out.println("String with Vowels duplicated: " + res);    }} // This code is contributed by// sanjeev2552
# Python3 program for printing String# with duplicate vowels  # Function to check for the Voweldef 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 duplicateddef 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 CodeS = "helloworld" # Print the original Stringprint("Original String: ", S) res = duplicateVowels(S) # Print the resultant Stringprint("String with Vowels duplicated: ", res) # This code is contributed by Mohit Kumar
// C# program for printing string// with duplicate vowelsusing System; class GFG{     // Function to check for the Vowel    static bool isVowel(char ch)    {        ch = char.ToUpper(ch);        return (ch == 'A' || ch == 'E' ||                ch == 'I' || ch == 'O' || ch == 'U');    }     // Function to get the resultant string    // with vowels duplicated    static 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    public static void Main(String[] args)    {        String str = "helloworld";         // Print the original string        Console.WriteLine("Original String: " + str);        String res = duplicateVowels(str);         // Print the resultant string        Console.WriteLine("String with Vowels duplicated: " + res);    }} // This code is contributed by Rajput-Ji
<script> // Javascript program for printing string// with duplicate vowels // Function to check for the Vowelfunction isVowel(ch){    ch = ch.toUpperCase();    return(ch == 'A' || ch == 'E' ||           ch == 'I' || ch == 'O' ||           ch == 'U');} // Function to get the resultant string// with vowels duplicatedfunction duplicateVowels(str){    let t = str.length;       // Another string to store    // the resultant string    let res = "";     // Loop to check for each character    for(let i = 0; i < t; i++)    {        if (isVowel(str[i]))            res += str[i];                     res += str[i];    }    return res;} // Driver Codelet str = "helloworld";   // Print the original stringdocument.write("Original String: " + str + "<br>");let res = duplicateVowels(str); // Print the resultant stringdocument.write("String with Vowels duplicated: " +               res + "<br>"); // This code is contributed by patel2127 </script>
Original String: helloworld
String with Vowels duplicated: heelloowoorld
 
mohit kumar 29
sanjeev2552
Rajput-Ji
patel2127
vowel-consonant
School Programming
Strings
Strings
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Constructors in Java
Exceptions in Java
Data Types
Inline Functions in C++
Pure Virtual Functions and Abstract Classes in C++
Write a program to reverse an array or string
Write a program to print all permutations of a given string
Longest Common Subsequence | DP-4
Check for Balanced Brackets in an expression (well-formedness) using Stack
KMP Algorithm for Pattern Searching