📜  修改并连接后打印给定的3个字符串

📅  最后修改于: 2021-05-31 23:46:58             🧑  作者: Mango

给定三个字符串(无空格)。任务是按如下所示修改三个给定的字符串后打印新字符串:

  • 第一个字符串存在的所有元音替换为“ *”。
  • 不要更改第二个字符串的任何内容。
  • 用“ $”替换第三个字符串中的所有辅音。
  • 连接所有三个字符串以获得新的字符串。

例子:

Input : how are you
Output : h*ware$ou

Input : geeks for geeks
Output : g**ksfor$ee$$

这个想法是遍历第一个字符串,并继续检查是否有任何字符是元音。更换这是元音带“*”的第一个字符串中的字符。同样,遍历第三个字符串并继续检查是否有任何字符不是元音。如果第三个字符串中的字符不是元音(当时它是一个辅音),以“$”代替它。

最后,连接三个字符串并打印新连接的字符串。

C++
// CPP program to modify the given strings
#include 
#include 
using namespace std;
  
// Function to modify the given three strings
string modifyStr(string str1, string str2, string str3)
{
    // Modifying first string
    for (int i = 0; i < str1.length(); i++) {
        if (str1[i] == 'a' || str1[i] == 'e' || 
            str1[i] == 'i' || str1[i] == 'o' || 
            str1[i] == 'u')
            str1[i] = '*';
    }
  
    // Modifying third string
    for (int i = 0; i < str3.length(); i++) {
        if (str3[i] != 'a' && str3[i] != 'e' && 
            str3[i] != 'i' && str3[i] != 'o' && 
            str3[i] != 'u')
            str3[i] = '$';
    }
  
    // Concatenating the three strings
    return (str1 + str2 + str3);
}
  
// Driver code
int main()
{
    string str1 = "how";
    string str2 = "are";
    string str3 = "you";
  
    cout << modifyStr(str1, str2, str3);
  
    return 0;
}


Java
// JAVA program to modify the given Strings
class GFG
{
  
// Function to modify the given three Strings
static String modifyStr(String str1, String str2, String str3)
{
    // Modifying first String
    for (int i = 0; i < str1.length(); i++) {
        if (str1.charAt(i) == 'a' || str1.charAt(i) == 'e' || 
            str1.charAt(i) == 'i' || str1.charAt(i) == 'o' || 
            str1.charAt(i) == 'u')
            str1 = str1.substring(0, i)+ '*' + 
                   str1.substring(i + 1);
    }
  
    // Modifying third String
    for (int i = 0; i < str3.length(); i++) {
        if (str3.charAt(i) != 'a' && str3.charAt(i) != 'e' && 
            str3.charAt(i) != 'i' && str3.charAt(i) != 'o' && 
            str3.charAt(i) != 'u')
            str3 = str3.substring(0, i)+ '$' + 
                   str3.substring(i + 1);
    }
  
    // Concatenating the three Strings
    return (str1 + str2 + str3);
}
  
// Driver code
public static void main(String[] args)
{
    String str1 = "how";
    String str2 = "are";
    String str3 = "you";
  
    System.out.print(modifyStr(str1, str2, str3));
}
}
  
// This code is contributed by 29AjayKumar


Python3
# Python3 program to modify the given Strings
  
# Function to modify the given three Strings
def modifyStr(str1, str2, str3):
      
    # Modifying first String
    for i in range(len(str1)):
        if (str1[i] == 'a' or str1[i] == 'e' or
            str1[i] == 'i' or str1[i] == 'o'
            or str1[i] == 'u'):
            str1 = str1[0:i] + '*' + str1[i + 1:];
      
  
    # Modifying third String
    for i in range(len(str3)):
        if (str3[i] != 'a' and str3[i] != 'e' and 
            str3[i] != 'i' and str3[i] != 'o'
            and str3[i] != 'u'):
            str3 = str3[0: i] + '$' + str3[i + 1:];
      
    # Concatenating the three Strings
    return (str1 + str2 + str3);
  
# Driver code
if __name__ == '__main__':
    str1 = "how";
    str2 = "are";
    str3 = "you";
  
    print(modifyStr(str1, str2, str3));
      
# This code is contributed by 29AjayKumar


C#
// C# program to modify the given Strings
using System;
  
class GFG
{
  
// Function to modify the given three Strings
static String modifyStr(String str1, String str2, String str3)
{
    // Modifying first String
    for (int i = 0; i < str1.Length; i++) 
    {
        if (str1[i] == 'a' || str1[i] == 'e' || 
            str1[i] == 'i' || str1[i] == 'o' || 
            str1[i] == 'u')
            str1 = str1.Substring(0, i)+ '*' + 
                str1.Substring(i + 1);
    }
  
    // Modifying third String
    for (int i = 0; i < str3.Length; i++)
    {
        if (str3[i] != 'a' && str3[i] != 'e' && 
            str3[i] != 'i' && str3[i] != 'o' && 
            str3[i] != 'u')
            str3 = str3.Substring(0, i)+ '$' + 
                str3.Substring(i + 1);
    }
  
    // Concatenating the three Strings
    return (str1 + str2 + str3);
}
  
// Driver code
public static void Main(String[] args)
{
    String str1 = "how";
    String str2 = "are";
    String str3 = "you";
  
    Console.Write(modifyStr(str1, str2, str3));
}
}
  
// This code is contributed by PrinciRaj1992


输出:
h*ware$ou
想要从精选的最佳视频中学习并解决问题,请查看有关从基础到高级C++的C++基础课程以及有关语言和STL的C++ STL课程。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”