0
input:-
1
Ans kot

Output:-
kot Ans

INPUT : the first line of the input contains the number of test cases. Each test case consists of a single line containing the string.

OUTPUT : output the string with the words swapped as stated above.**

Code:-

Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
StringBuffer result = new StringBuffer();
for (int i = 0; i < a; i++) {
    String b = sc.next();
    String my[] = b.split(" ");
    StringBuffer r = new StringBuffer();
    for (int j = my.length - 1; j > 0; j--) {
        r.append(my[j] + " ");
    }
    r.append(my[0] + "\n");
    result.append(r.toString());
}
System.out.println(result.toString());
}

What is wrong in my code ? above is code which i am trying.

ansh
  • 67
  • 1
  • 5

7 Answers7

2
String my[] = b.split(" ");
StringBuffer r = new StringBuffer();
for (int j = my.length - 1; j > 0; j--) {
    r.append(my[j] + " ");
}

this snippet of your code is only gonna reverse the sentence "word by word" not "character by character". therefore, you need reverse the string (my[j]) before you append it into the StringBuffer

kucing_terbang
  • 4,487
  • 2
  • 19
  • 27
1

Use this

 Scanner sc = new Scanner(System.in);
            int a = sc.nextInt();
            sc.nextLine();
            StringBuffer result = new StringBuffer();
            for (int i = 0; i < a; i++) {
                String b = sc.nextLine();
                String my[] = b.split(" ");
                StringBuffer r = new StringBuffer();
                for (int j = my.length - 1; j > 0; j--) {
                    r.append(my[j] + " ");

                }
                r.append(my[0] + "\n");
                result.append(r.toString());
            }
            System.out.println(result.toString());
        }
Nightswatch
  • 111
  • 1
  • 5
  • But why with next it is not working... and nextLine it is working... can u explain it – ansh Jul 13 '15 at 07:33
  • Using next() will only return what comes before a space. nextLine() automatically moves the scanner down after returning the current line. – Nightswatch Jul 13 '15 at 07:35
  • http://stackoverflow.com/questions/22458575/whats-the-difference-between-next-and-nextline-methods-from-scanner-class – Nightswatch Jul 13 '15 at 07:35
0

Takes String input and return String in reverse order of each characters.

String reverse(String x) {
        int i = x.length() - 1;
        StringBuilder y = new StringBuilder();
        while (i >= 0) {
            y.append(x.charAt(i));
            i--;
        }

        return y.toString();
    }
Ravi
  • 28,657
  • 41
  • 110
  • 158
0
public static String reverseWords(String input) {
    Deque<String> words = new ArrayDeque<>();
    for (String word: input.split(" ")) {
        if (!word.isEmpty()) {
            words.addFirst(word);
        }
    }
    StringBuilder result = new StringBuilder();
    while (!words.isEmpty()) {
        result.append(words.removeFirst());
        if (!words.isEmpty()) {
            result.append(" ");
        }
    }
    return result.toString();
}
My God
  • 21,961
  • 23
  • 93
  • 166
0

You can run this code:

String[] splitted = yourString.split(" ");

for (int i = splitted.length-1; i>=0; i--){
    System.out.println(splitted[i]);
} 
The Dr.
  • 635
  • 1
  • 10
  • 24
0

Multiple things:

  1. You are using next api which will just read your string that you type word by word and you loop until a i.e. in your example just once. So instead use nextLine api which will read whole line instead of just a word and then split by space:

    String b = sc.nextLine();
    
  2. You are reading input with nextInt api followed by enter, you you might sometime end up having return character when reading next token using next api. Instead use:

    int a = Integer.parseInt(sc.nextLine());
    
  3. You are using StringBuffer which has an overhead of obtaining mutex and hence should use StringBuilder.
SMA
  • 33,915
  • 6
  • 43
  • 65
0

Code:-
               
    Scanner sc =new Scanner(System.in);
        int a =Integer.parseInt(sc.nextLine());
        StringBuffer result= new StringBuffer();
        for (int i = 0; i <a; i++) {
     String b=sc.nextLine();
        String my[]= b.split(" ");
        StringBuffer r = new StringBuffer();
     for (int j = my.length-1; j >0; j--) {
      r.append(my[j]+" ");
      
     }
     r.append(my[0] + "\n");
          result.append(r.toString());
    }
     System.out.println(result.toString());
   

enter code here
Sudhir
  • 403
  • 3
  • 18