Dadas tres strings (sin espacios). La tarea es imprimir la nueva string después de modificar las tres strings dadas de la siguiente manera:
- Reemplace todas las vocales presentes en la primera string con «*».
- No cambies nada en la segunda string.
- Reemplace todas las consonantes en la tercera string con «$».
- Concatene las tres strings para obtener la nueva string.
Ejemplos:
Input : how are you Output : h*ware$ou Input : geeks for geeks Output : g**ksfor$ee$$
La idea es recorrer la primera string y seguir comprobando si algún carácter es vocal o no. Reemplace el carácter en la primera string que es vocal con «*». Del mismo modo, recorra la tercera string y siga comprobando si algún carácter no es una vocal. Si un carácter en la tercera string no es una vocal (entonces es una consonante), reemplácelo con ‘$’.
Finalmente, concatene las tres strings e imprima la string recién concatenada.
C++
// CPP program to modify the given strings #include <iostream> #include <string.h> 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
Javascript
<script> // Javascript program to modify the given Strings // Function to modify the given three Strings function modifyStr(str1, str2, str3) { // Modifying first String for(var 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(var 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 var str1 = "how"; var str2 = "are"; var str3 = "you"; document.write(modifyStr(str1, str2, str3)); // This code is contributed by Ankita saini </script>
h*ware$ou
Publicación traducida automáticamente
Artículo escrito por Smitha Dinesh Semwal y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA