Program to duplicate Vowels in String
Last Updated :
21 Dec, 2022
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++
// 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;
}
Java
// Java program for printing string
// with duplicate vowels
import 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
# 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
C#
// C# program for printing string
// with duplicate vowels
using 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
JavaScript
<script>
// Javascript program for printing string
// with duplicate vowels
// Function to check for the Vowel
function isVowel(ch)
{
ch = ch.toUpperCase();
return(ch == 'A' || ch == 'E' ||
ch == 'I' || ch == 'O' ||
ch == 'U');
}
// Function to get the resultant string
// with vowels duplicated
function 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 Code
let str = "helloworld";
// Print the original string
document.write("Original String: " + str + "<br>");
let res = duplicateVowels(str);
// Print the resultant string
document.write("String with Vowels duplicated: " +
res + "<br>");
// This code is contributed by patel2127
</script>
Output: Original String: helloworld
String with Vowels duplicated: heelloowoorld
Time Complexity: O(N), Here N is the length of the string
Auxiliary Space: O(N), The extra space is used to store the result.
Similar Reads
C program to count number of vowels and consonants in a String Given a string and write a C program to count the number of vowels and consonants in this string. Examples: Input: str = "geeks for geeks" Output: Vowels: 5 Consonants: 8 Input: str = "abcdefghijklmnopqrstuvwxyz" Output: Vowels: 5 Consonants: 21Using For LoopsTake the string as inputTake each charac
4 min read
Reverse vowels in a given string Given a string s, reverse only the vowels in s while keeping the other characters in their original positions.Examples:Input: "geeksforgeeks"Output: "geeksforgeeks"Explanation: The vowels 'e', 'e', 'o', 'e', 'e' are reversed, resulting in "geeksforgeeks".Input: "helloworld"Output: "hollowerld"Explan
9 min read
Print reverse string after removing vowels Given a string s, print reverse of string and remove the characters from the reversed string where there are vowels in the original string. Examples: Input : geeksforgeeksOutput : segrfsegExplanation :Reversed string is skeegrofskeeg, removing characters from indexes 1, 2, 6, 9 & 10 (0 based ind
13 min read
Program to accept Strings starting with a Vowel Given string str consisting of alphabets, the task is to check whether the given string is starting with a Vowel or Not. Examples: Input: str = "Animal" Output: Accepted Input: str = "GeeksforGeeks" Output: Not Accepted Approach: Find the first character of the stringCheck if the first character of
4 min read
Count the pairs of vowels in the given string Given a string str consisting of lowercase English alphabets, the task is to count the number of adjacent pairs of vowels.Examples: Input: str = "abaebio" Output: 2 (a, e) and (i, o) are the only valid pairs.Input: str = "aeoui" Output: 4 Approach: Starting from the first character of the string to
5 min read