📌  相关文章
📜  在给定的字符串中将元音转换为大写字符

📅  最后修改于: 2021-04-27 18:38:34             🧑  作者: Mango

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

例子:

方法:想法是遍历给定字符串的字符,并检查当前字符是否为小写字符和元音。如果发现为真,则将字符转换为大写。请按照以下步骤解决问题:

  • 遍历给定的字符串,并检查str [i]是否为小写元音。
  • 如果发现是真的,则将str [i]替换为(str [i] –’a’+’A’)
  • 最后,在完成字符串的遍历之后,打印最终的字符串。

下面是该方法的实现:

C++
// C++ program to implment
// 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 implment
// 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 implment
// 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


输出:
EUtOpIA











时间复杂度: O(N)
辅助空间: O(1)