Reverse a Sentence in Java

Learn via video course
FREE
View all courses
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Topics Covered

Strings in Java are used to store a sequence of characters that form sentences. These sentences are a group of words separated by spaces. For example - "You are sweet". It is a sentence made up of three words. However, after reversing this sentence, the output becomes "sweet are You".

Therefore, reversing a sentence means reversing the order of the words. We store these sentences in our Java programs using the String data type.

Introduction

As discussed above, reversing a sentence means reversing the order of the words present. Flip the sentence around: move the first word to the end, the last word to the beginning, the second word to the second-to-last position, and vice versa. Repeat this process until the entire sentence is reversed.

The following example makes it more clear - introduction of reverse a sentence in java

This problem can be approached in different ways, such as using recursion, a split() function in Java with a loop, and performing basic logic building.

Reverse a Sentence in Java Using Recursion

Recursion means a function calling itself. We require three things to solve a problem using recursion: A base condition. A recursive call to the function. Some logic to solve our given problem.

  • For reversing a sentence, we will find the index of our whitespaces between two consecutive words. These whitespaces will help us to extract a particular word from our sentence.
  • We are going to break our sentence at this index of the whitespaces. The left of the sentence will contain the word to be added in the last, whereas the right of the sentence will be passed in the recursive call.
  • However, when no whitespaces are left in our sentence, we should simply return the string to add the last word. This would be our base case.

The function would be called like this:

Reverse a Sentence in Java Using Recursion

Let us see how it will be useful in reversing our string in Java.

Output:

In the above code, we are taking the input; that is, the string to be reversed is stored in the variable str, after which we call a function reverse() to find the reverse of the string. In each iteration of the reverse function, we concatenate the result of the reverse() function with the first word of the sentence using s.substring(0,x).

The reverse() function must be called before s.substring(0,x) because that way, the words will be added to the last in the result. However, if you reverse the order, you will get the same sentence again. In the last iteration, we get the variable x = -1, which is the base condition according to which the reverse() function will return our reversed sentence. It is to note that the indexOf() method returns the position of the first occurrence of a specified character(s) in a string, if not present returns 1-1.

Time Complexity: O(N)O(N) Space Complexity: O(N)O(N)

Reverse a Sentence in Java Using Loop

There are two methods to reverse a sentence using a loop. Let us see them in detail.

Reverse a Sentence Using split() Function

The split() function in Java breaks a sentence based on a certain delimiter provided to it as the arguments. The split() function is a built-in function in Java. After breaking the sentence into words, these words are added one by one in the last to reverse a sentence.

Code for reversing a sentence using split() function -

Output:

In the above program, the sentence is broken into an array of words using the space as a delimiter. After using the split() function, our s array will contain -

Now, we loop over this s array and add these words in reverse order. However, the rev variable should be written after the array element, that is, s[i], so as to reverse the sentence. If you write the rev variable before s[i], you will get the original sentence as your answer.

In the end, we print our result.

Time Complexity: O(N)O(N) Space Complexity: O(N)O(N)

Reverse a Sentence in Java Using whitespaces

In this method, we form the individual words using the whitespaces present in the sentence. Until we find a space in the sentence, we keep on adding the characters from the sentence in a variable, and as soon as we find a space, we add this word in our rev variable. However, after adding this word, we again set the word to null to form a new word.

Let us see the code for reversing the sentence using loop and whitespaces -

Output:

In the above program, we have explicitly added a space at the end of the sentence because every word is extracted based on a space present after it, but the last word has no space after it.

Now, we loop over our sentence and extract each character using the charAt() function. We keep on adding the characters to word variable until we find a space. However, as soon as we find a space, we are sure that one word has been formed and add it to the rev variable.

If we write the word variable after rev, you might end up with the original sentence. After adding this word to the rev variable, we initialize the word variable to null again to store another word in it.

At last, print the reversed sentence.

Time Complexity: O(N)O(N) Space Complexity: O(1)O(1)

where NN is the length of the sentence.

Conclusion

  • Reverse a sentence means to reverse the words of a sentence in the opposite order.
  • We have used recursion to reverse the sentence in Java by finding the index of the first whitespace and breaking our sentence based on this index. The left part of the sentence is added to our result, whereas the right part is passed as to the recursive call to reverse it again.
  • We also have used the split() function, in which we split our sentences based on the whitespaces present in our string and then added them in reverse order using a for a loop.
  • In all our approaches, we have to split our sentences using the whitespaces in our string. Therefore, whitespace plays a very important role in this problem.