Computer Science, asked by ilawanrishisha, 7 months ago

4. Write a program in Java to accept two strings. Display the new string by taking each
character of the first string from left to right and of the second string from right to left.
The letters should be taken alternatively from each string. Assume that the length of
both the strings are the same.
Sample input String 1: HISTORY
String 2: SCIENCE
Output -
HEICSNTEOIRCYS
Rabatid hafi​

Answers

Answered by June021
2

Answer:

class A{

public static void main (String args[])

{

Scanner sc=new Scanner(System.in);

String str1=sc.nextLine();

String str2=sc.nextLine();

if(str1.length()==str2.length())

{

int flen=str1.length()+str2.length();

for(int i=0, int j=flen, int k=0; i<flen/2, j>=0,k<flen; i+2,j-2,k++)

{

arr[k]=str1.charAt(i);

k=k+1;

arr[k]=str2.charAt(i);

}

}

for(int i=0; i<flen: i++)

{

System.out.println(arr[i]);

}

}

}

Explanation:

I hope this should work....

Used the for loop with 3 variables...

flen is the summation of two string lengths...

Hope this solves....

Similar questions