Given a sentence the task is to form a new sentence by reversing each word of the sentence.
Example: “Computer Science”. The expected output is “retupmoC ecneicS”
Java
import java.util.*;
public class ReverseSentenceWord
{
public static void main(String args[])
{
String str="",nstr="",wrd="";
char ch=' ';
int l=0;
Scanner sc=new Scanner(System.in);
System.out.print("Enter a sentence: ");
str=sc.nextLine();
str=str.trim()+" ";
l=str.length();
for(int i=0;i<l;i++)
{
ch=str.charAt(i);
if(ch==' ')
{
nstr=nstr+wrd+' ';
wrd="";
}
else
{
wrd=ch+wrd;
}
}
System.out.println("New string: "+nstr);
}
}Java