Print the given 3 string after modifying and concatenating
Last Updated :
13 Mar, 2023
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$ou
Input: 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:
C++
#include <iostream>
#include <string.h>
using namespace std;
string modifyStr(string str1, string str2, string str3)
{
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] = '*' ;
}
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] = '$' ;
}
return (str1 + str2 + str3);
}
int main()
{
string str1 = "how" ;
string str2 = "are" ;
string str3 = "you" ;
cout << modifyStr(str1, str2, str3);
return 0;
}
|
Java
class GFG
{
static String modifyStr(String str1, String str2, String str3)
{
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 );
}
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 );
}
return (str1 + str2 + str3);
}
public static void main(String[] args)
{
String str1 = "how" ;
String str2 = "are" ;
String str3 = "you" ;
System.out.print(modifyStr(str1, str2, str3));
}
}
|
Python3
def modifyStr(str1, str2, str3):
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 :];
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 :];
return (str1 + str2 + str3);
if __name__ = = '__main__' :
str1 = "how" ;
str2 = "are" ;
str3 = "you" ;
print (modifyStr(str1, str2, str3));
|
C#
using System;
class GFG
{
static String modifyStr(String str1, String str2, String str3)
{
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);
}
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);
}
return (str1 + str2 + str3);
}
public static void Main(String[] args)
{
String str1 = "how" ;
String str2 = "are" ;
String str3 = "you" ;
Console.Write(modifyStr(str1, str2, str3));
}
}
|
Javascript
<script>
function modifyStr(str1, str2, str3)
{
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);
}
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);
}
return (str1 + str2 + str3);
}
var str1 = "how" ;
var str2 = "are" ;
var str3 = "you" ;
document.write(modifyStr(str1, str2, str3));
</script>
|
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.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...