pages:
  • 1
WhiteRose SAYS

why my code is wrong? i don't understand!!

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

  Scanner in = new Scanner(System.in);
  int l = in.nextInt();
  while (l-- > 0) {
     int t = in.nextInt();
     in.nextLine();
     while (t-- > 0) {
        String[] str = in.nextLine().split(" ");
        String r = "";
        for (String str1 : str) {
           for (int j = str1.length() - 1; j >= 0; j--) {
              r += str1.charAt(j);
           }
           if (!str1.equals(str[str.length - 1])) {
              r += " ";
           }
        }
        System.out.println(r);
     }
     if (l != 0) {
        System.out.println();
     }
  }

} }

M.Khooryani SAYS
if (!str1.equals(str[str.length - 1])) {
       r += " ";
 }

"To be or not to be"

str: {"To", "be", "or", "not", "to", "be"}

"be" is equal to last string in array("be"), so

r += " "

ignored and your output is:

oT ebro ton ot eb

instead of

oT eb ro ton ot eb