Two Strings Interchange Vowels
Two Strings Interchange Vowels
PROBLEM STATEMENT :
The program must accept two string values S1, S2 (both having equal number of vowels) and interchange the vowels in the string values in their order of occurrence.
Boundary Condition(s):
1 <= Length of S1, S2 <= 1000
Input Format:
The first line contains S1.
The second line contains S2.
Output Format:
The first line contains S1 with the vowels interchanged.
The second line contains S2 with the vowels interchanged.
Example Input/Output 1:
Input: ()
apple
rain
Output:
appli
raen
Explanation:
Here S1 = apple and S2 = rain.
After interchanging the vowels in the string values in their order of occurrence, the string values become
S1 = appli and S2 = raen.
Example Input/Output 2:
Input: ()
PANCAKE
Umbrella
Output:
PUNCeKa
AmbrAllE SOLUTION :
C (Programming Language)
#include<stdio.h>
#include<stdlib.h>
int isvowel(char w){
char k=tolower(w);
if(k=='a'||k=='e'||k=='i'||k=='o'||k=='u')
return 1;
return 0;
}
int main()
{
char str1[1000],str2[1000],t;
scanf("%s\n%s",str1,str2);
int i,j,k=0;
for(i=0;i<strlen(str1);i++){
if(isvowel(str1[i])){
while(isvowel(str2[k])==0){
k++;
}
t=str1[i];
str1[i]=str2[k];
str2[k]=t;
k++;
}
}
printf("%s\n%s",str1,str2);
}
C++ (CPP)
#include <bits/stdc++.h>
using namespace std;
int main(int argc, char** argv)
{
int i,j=-1;
string s1,s2;
cin>>s1>>s2;
for(i=0;i<s1.size();++i)
{
if(strchr("aeiou",tolower(s1[i])))
{
while(!strchr("aeiou",tolower(s2[++j])));
swap(s1[i],s2[j]);
}
}
cout<<s1<<endl<<s2;
}
JAVA
import java.util.*;
public class Hello {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String s=sc.next();
String t=sc.next();
String src="AEIOUaeiou";
char ch[]=s.toCharArray();
char ch1[]=t.toCharArray();
int start=0;
for(int i=0;i<ch.length;i++){
if(src.contains(String.valueOf(ch[i]))){
for(int j=start;j<ch1.length;j++){
if(src.contains(String.valueOf(ch1[j]))){
char temp=ch[i];
ch[i]=ch1[j];
ch1[j]=temp;
start=j+1;
break;
}
}
}
}
String n=String.valueOf(ch);
String n1=String.valueOf(ch1);
System.out.printf("%s\n%s",n,n1);
}
}
PYTHON
s1=list(input().strip())
s2=list(input().strip())
l1=len(s1);l2=len(s2);j=0
for i in range(l1):
if s1[i].lower() in "aeiou":
while j<l2 and s2[j].lower() not in "aeiou":
j+=1
s1[i],s2[j]=s2[j],s1[i]
j+=1
print(''.join(s1),''.join(s2),sep='\n')
Comments
Post a Comment