定的字符串str,任务是把所有本元音给定的字符串中大写字符指定的字符串中。
例子:
Input: str = “GeeksforGeeks”
Output: GEEksfOrGEEks
Explanation:
Vowels present in the given string are: {‘e’, ‘o’}
Replace ‘e’ to ‘E’ and ‘o’ to ‘O’.
Therefore, the required output is GEEksfOrGEEks.
Input: str = “eutopia”
Output: EUtOpIA
方法:我们的想法是迭代指定字符串的字符,并检查当前字符是一个小写字符和一个元音或没有。如果发现为真,则将字符转换为大写。请按照以下步骤解决问题:
- 遍历给定的字符串并检查str[i]是否是小写元音。
- 如果发现为真,请将str[i]替换为(str[i] – ‘a’ + ‘A’) 。
- 最后,在完成对字符串的遍历后,打印最终的字符串。
下面是该方法的实现:
C++
// C++ program to implement
// the above approach
#include
using namespace std;
// Function to convert vowels
// into uppercase
string conVowUpp(string& str)
{
// Stores the length
// of str
int N = str.length();
for (int i = 0; i < N; i++) {
if (str[i] == 'a' || str[i] == 'e'
|| str[i] == 'i' || str[i] == 'o'
|| str[i] == 'u') {
str[i] = str[i] - 'a' + 'A';
}
}
return str;
}
// Driver Code
int main()
{
string str = "eutopia";
cout << conVowUpp(str);
}
Java
// Java program to implement
// the above approach
import java.util.*;
class GFG{
// Function to convert vowels
// into uppercase
static void conVowUpp(char[] str)
{
// Stores the length
// of str
int N = str.length;
for (int i = 0; i < N; i++)
{
if (str[i] == 'a' || str[i] == 'e' ||
str[i] == 'i' || str[i] == 'o' ||
str[i] == 'u')
{
char c = Character.toUpperCase(str[i]);
str[i] = c;
}
}
for(char c : str)
System.out.print(c);
}
// Driver Code
public static void main(String[] args)
{
String str = "eutopia";
conVowUpp(str.toCharArray());
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 program to implement
# the above approach
# Function to convert vowels
# into uppercase
def conVowUpp(str):
# Stores the length
# of str
N = len(str)
str1 =""
for i in range(N):
if (str[i] == 'a' or str[i] == 'e' or
str[i] == 'i' or str[i] == 'o' or
str[i] == 'u'):
c = (str[i]).upper()
str1 += c
else:
str1 += str[i]
print(str1)
# Driver Code
if __name__ == '__main__':
str = "eutopia"
conVowUpp(str)
# This code is contributed by Amit Katiyar
C#
// C# program to implement
// the above approach
using System;
class GFG{
// Function to convert vowels
// into uppercase
static void conVowUpp(char[] str)
{
// Stores the length
// of str
int N = str.Length;
for (int i = 0; i < N; i++)
{
if (str[i] == 'a' || str[i] == 'e' ||
str[i] == 'i' || str[i] == 'o' ||
str[i] == 'u')
{
char c = char.ToUpperInvariant(str[i]);
str[i] = c;
}
}
foreach(char c in str)
Console.Write(c);
}
// Driver Code
public static void Main(String[] args)
{
String str = "eutopia";
conVowUpp(str.ToCharArray());
}
}
// This code is contributed by shikhasingrajput
Javascript
输出:
EUtOpIA
时间复杂度: O(N)
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live