本文概述
- C ++
- Java
- Python3
- C#
例子:
Input: str = "geeks"
Output: geeeeksInput: str = "java"
Output: jaavaa
方法:使用循环迭代字符串。检查字符是否为元音,并重复它。返回并打印结果字符串。
【算法设计(在字符串中复制元音)】下面是上述方法的实现:
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
输出如下:
Original String: helloworld
String with Vowels duplicated: heelloowoorld
推荐阅读
- 检查数字奇数位的数字总和是否可被K整除
- 地理信息系统(GIS)中数据的特征
- C语言中的运算符|S1(算术运算符)
- 错误检测代码–校验和是什么()
- Nodejs GM drawLine()函数用法介绍
- win7系统重装后没有声音的处理办法
- win7开机慢怎样办?win7开机慢的处理办法
- 如何激活win7?win7激活图文步骤
- windows7 激活工具怎样运用?