
To reverse a every other word in a string.
Example :- If the input is "This is a line of message" the output should be "sihT is a line fo message"

import java.util.*;
public class ReverseString{
public static void main(String[]args){
Scanner input=new Scanner(System.in);
StringBuffer buffer=new StringBuffer();
System.out.println("Enter string: ");
String str=input.nextLine();
StringTokenizer stk=new StringTokenizer(str);
while(stk.hasMoreTokens()){
String st=stk.nextToken();
String reverse = new StringBuffer(st).reverse().toString();
buffer.append(reverse);
buffer.append(" ");
}
System.out.println("Reverse: " + buffer.toString());
}
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.