Reverse Words in a Sentence
E · easyP · Verified PYQstrings
Problem
Given a sentence, reverse each word individually while keeping the word order intact. Example: “Hello World” becomes “olleH dlroW”
Example
Input
Hello World TCS
Output
olleH dlroW SCT
Split by space, reverse each word using slicing [::-1], join back with space.
javamay contain transcription errors
import java.util.*;
class Main{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
String str=sc.nextLine();
String[] words=str.split(" ");
for(int i=words.length-1;i>=0;i--){
System.out.print(words[i]+" ");
}
}
}