Given a sentence the task is to reverse the whole sentence without reversing each word.
Example: Consider a sentence “Lorem Ipsum is simply dummy text”. On reversing the sentence, we get “text dummy simply is Ipsum Lorem”.
Java
import java.util.*;
public class SentenceReverse
{
public static void main(String args[])
{
int l=0;
char ch=' ';
String str="",nstr="",word="";
Scanner sc=new Scanner(System.in);
System.out.print("Enter the sentence: ");
str=sc.nextLine();
str=str.trim();
str=str+" ";
l=str.length();
for (int i=0;i<l;i++)
{
ch=str.charAt(i);
if(ch==' ')
{
nstr=word+" "+nstr;
word="";
}
else
{
word=word+ch;
}
}
System.out.print("Reverse of the above sentence: "+nstr);
}
}Java