📜  将给定字符串的元音转换为大写字符

📅  最后修改于: 2021-09-07 05:19:40             🧑  作者: Mango

字符串str,任务是把所有本元音给定的字符串中大写字符指定的字符串中。

例子:

方法:我们的想法是迭代指定字符串的字符,并检查当前字符是一个小写字符和一个元音或没有。如果发现为真,则将字符转换为大写。请按照以下步骤解决问题:

  • 遍历给定的字符串并检查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