- codewithshiva

Latest

Search Bar

Monday, July 12, 2021

Reverse String again

Description

Reverse the string again this time but maintain the relative order of the words and the spaces inbetween.

Input

1<=T<=10

each of the next T lines consist of a single string.

Outputimport java.util.Scanner;

public class Main {


    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        int times = sc.nextInt();

        sc.nextLine(); //only for adding sc.nextLine effective


        for (int i = 0;i<times;i++){//feeding array -->

            String str = sc.nextLine();

            int len = str.length();

            int index = 0;

            int count = -1;


            for (int j = 0;j<len;j++){//looping full [str]

                char c = str.charAt(j);

                if(c==' '){

                    for (int k  = j-1;k>=index;k--){

                        System.out.print(str.charAt(k));

                    }

                    System.out.print(" ");

                    index = j+1;

                    count = 0;

                }else {

                    count++;

                }

            }for (int last = len-1;last>=index;last--){

                System.out.print(str.charAt(last));

            }System.out.println();

        }

    }

}

output a single string , the answer to the problem.

Sample Input 1 

3
hi there
hello  world
a  b

Sample Output 1

ih ereht
olleh  dlrow
a  b

No comments:

Post a Comment