I am a beginner and I want to write a program that takes a string from the user and reverses every word but not the whole sentence. For example if "gnimmargorP si nuf" is the input, output should be "Programming is fun". not "fun is programming" what can I do or change to make the program see the words of the sentences
I wrote:
import java.util.Scanner;
public class Lab06
{
public static void main( String[] args )
{
Scanner scan = new Scanner( System.in );
String line;
// REVERSER
System.out.println();
System.out.println("Please enter a line");
line = scan.nextLine();
System.out.println( "The reverse of the sentence is:");
System.out.println(reverse(line));
}
public static String reverse( String s )
{
String answer = "";
int length = s.length();
for ( int i = length - 1; i >= 0; i--)
{
answer = answer + s.charAt(i);
}
return answer;
}
}