Back
I am preparing for a Java interview and I want to reverse order a string containing a sentence. How can I do it?
You can split the sentence and reverse it like this:
String str=”The Avengers is a great film!”;String[] splitter=str.split(“ “);String result=””;for(int i=splitter.length-1;i>=0;i--){result+=(splitter[i]+” “);}System.out.println(result.trim());
String str=”The Avengers is a great film!”;
String[] splitter=str.split(“ “);
String result=””;
for(int i=splitter.length-1;i>=0;i--){
result+=(splitter[i]+” “);
}
System.out.println(result.trim());
Check out the java interview questions from Intellipaat.
31k questions
32.8k answers
501 comments
693 users