Print the given 3 string after modifying and concatenating
Given three strings(without spaces). The task is to print the new string after modifying the three given string as follows:
- Replace all the vowels present in the first string with "*".
- Don't change anything in the second string.
- Replace all the consonants in the third string with "$".
- Concatenate all of the three string to obtain the new string.
Examples:
Input: how are you
Output\: h*ware$ouInput: geeks for geeks
Output: g**ksfor$ee$$
Approach:
The idea is to traverse the first string and keep checking if any character is a vowel or not. Replace the character in the first string which is vowel with "*". Similarly, traverse the third string and keep checking if any character is not a vowel. If a character in the third string is not a vowel(then it is a consonant), replace it with '$'.
Finally, concatenate the three strings and print the newly concatenated string.
Below is the implementation of the above approach:
// 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 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 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# 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
<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>
Output:
h*ware$ou
Time Complexity: O(m+n), where m is the length of the first string and n is the length of the third string.
Auxiliary Space: O(1), as constant extra space is required.