Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (7k points)

I am preparing for a Java interview and I want to reverse order a string containing a sentence. How can I do it?

1 Answer

0 votes
by (13.1k points)

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());

Check out the java interview questions from Intellipaat.

Related questions

0 votes
1 answer
asked May 7, 2021 in Java by sheela_singh (9.5k points)
0 votes
1 answer
asked May 15, 2021 in Java by sheela_singh (9.5k points)
0 votes
1 answer

Browse Categories

...