0

I have a question about the practice problem of GFG, The question is given below:-

Given a string S as input. You have to reverse the given string.

Input: The first line of input contains a single integer T which denotes the number of test cases. T test cases follows, first line of each test case contains a string S.

Output: Corresponding to each test case, print the string S in reverse order.

I also attached solution of this but when I compile the code it only print one test case not all the test case.

For Input:
2 YSlxoBxAqYvIe4HxC4qO hibxuvax1MwiWYQzDEf2

your output is: Oq4CxH4eIvYqAxBoxlSY

    import java.util.*;
import java.lang.*;
import java.io.*;

class GFG {
    public static void main (String[] args) {
        //code
        Scanner in = new Scanner(System.in);
        int T = in.nextInt();
        for(int j = 1; j<=T; j++) {
        String S = in.nextLine();
        char temp;
        char[] arr = S.toCharArray();
        int len = arr.length;
        for(int i=0; i<(S.length())/2; i++,len--){
            temp = arr[i];
            arr[i] = arr[len-1];
            arr[len-1] = temp;
        }
        System.out.println(String.valueOf(arr));
        }
    }
}
Vishal
  • 1
  • 2
  • `nextInt()` doesn't pass the cursor to next line but rather stays on the same line. `Integer.parseInt(in.nextLine())` should solve your problem. – nice_dev Oct 15 '20 at 05:22
  • Rather than doing that, simply use `S.toCharAt(i)` and print – papaya Oct 15 '20 at 05:23
  • I'm not able to understand you guys. Will you guys please elaborate it. Because I am a beginner. – Vishal Oct 15 '20 at 05:34

0 Answers0