204k views
3 votes
Write a Java program that generates a new string by concatenating the reversed substrings of even indexes and odd indexes separately from a given string.

1 Answer

6 votes

Answer:

/ReversedEvenOddString.java

import java.util.Scanner;

public class ReversedEvenOddString {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

String s = sc.nextLine();

String evens = "";

String odds = "";

for(int i = s.length()-1;i>=0;i--){

if(i%2==1){

odds += s.charAt(i);

}

else{

evens += s.charAt(i);

}

}

String res;

if(s.length()%2==1){

res = evens+odds;

}

else{

res = odds+evens;

}

System.out.println(res);

}

}

Write a Java program that generates a new string by concatenating the reversed substrings-example-1
User Whisperstream
by
4.3k points