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++
#include <bits/stdc++.h>
using namespace std;
bool isVowel( char ch)
{
ch = toupper (ch);
return (ch == 'A' || ch == 'E'
|| ch == 'I' || ch == 'O'
|| ch == 'U' );
}
string duplicateVowels(string str)
{
int t = str.length();
string res = "" ;
for ( int i = 0; i < t; i++) {
if (isVowel(str[i])) {
res += str[i];
}
res += str[i];
}
return res;
}
int main()
{
string str = "helloworld" ;
cout << "Original String: "
<< str << endl;
string res = duplicateVowels(str);
cout << "String with Vowels duplicated: "
<< res << endl;
}
|
Java
import java.util.*;
class GFG
{
static boolean isVowel( char ch)
{
ch = Character.toUpperCase(ch);
return (ch == 'A' || ch == 'E' ||
ch == 'I' || ch == 'O' || ch == 'U' );
}
static String duplicateVowels(String str)
{
int t = str.length();
String res = "" ;
for ( int i = 0 ; i < t; i++)
{
if (isVowel(str.charAt(i)))
res += str.charAt(i);
res += str.charAt(i);
}
return res;
}
public static void main(String[] args)
{
String str = "helloworld" ;
System.out.println( "Original String: " + str);
String res = duplicateVowels(str);
System.out.println( "String with Vowels duplicated: " + res);
}
}
|
Python3
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
def duplicateVowels(S):
t = len (S)
res = ""
for i in range (t):
if (isVowel(S[i])):
res + = S[i]
res + = S[i]
return res
S = "helloworld"
print ( "Original String: " , S)
res = duplicateVowels(S)
print ( "String with Vowels duplicated: " , res)
|
C#
using System;
class GFG
{
static bool isVowel( char ch)
{
ch = char .ToUpper(ch);
return (ch == 'A' || ch == 'E' ||
ch == 'I' || ch == 'O' || ch == 'U' );
}
static String duplicateVowels(String str)
{
int t = str.Length;
String res = "" ;
for ( int i = 0; i < t; i++)
{
if (isVowel(str[i]))
res += str[i];
res += str[i];
}
return res;
}
public static void Main(String[] args)
{
String str = "helloworld" ;
Console.WriteLine( "Original String: " + str);
String res = duplicateVowels(str);
Console.WriteLine( "String with Vowels duplicated: " + res);
}
}
|
Javascript
<script>
function isVowel(ch)
{
ch = ch.toUpperCase();
return (ch == 'A' || ch == 'E' ||
ch == 'I' || ch == 'O' ||
ch == 'U' );
}
function duplicateVowels(str)
{
let t = str.length;
let res = "" ;
for (let i = 0; i < t; i++)
{
if (isVowel(str[i]))
res += str[i];
res += str[i];
}
return res;
}
let str = "helloworld" ;
document.write( "Original String: " + str + "<br>" );
let res = duplicateVowels(str);
document.write( "String with Vowels duplicated: " +
res + "<br>" );
</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.
Last Updated :
21 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...