Reverse Words In A String Java Without Using Functions
Last Updated : Mar 11, 2024
In this article we will show you the solution of reverse words in a string java without using functions, an array of characters is first created from the string.
A reversed array is then created. The reversed array is used to create a new string.
It is impossible to modify String objects. While the String class in Java lacks a reverse() method, it is equipped with a reverse() method in the StringBuilder class.
ToCharArray() is not available in StringBuilder class, while it is available in String class.
For loops are run from index 0 to the length of the set. In the case of charAt(index), each index is inserted into the new string revString using charAt(index).
This method returns all characters to the revString string, which is added to the string returned by charAt().
In the end, the revString string will have the reversed string after the for loop is completed.
It is ideal to have a solution like this, as it is very good and optimal.
On the basis of a condition, we will call the method printReverse() from the same method. By using if condition, this condition can be controlled.
By avoiding for loops, core programming can be done more efficiently.
In the absence of a good condition in the if statement, the printReverse method will be called continuously.
This is the drawback of recursive. Using recursion logic on any program is no problem if you are confident.
There is a third way to solve this problem, but it is just one more way to avoid using String reverse.
There is a possibility that some interviewers would not accept this solution.
Mutable and final, the StringBuilder class is a class that builds strings. Using this method, it is possible to reverse the content.
Splitting the input string into words is easy when you use the split() method.
A string is split around regular expression matches using the string split() method. String arrays are returned after splitting.
- Create a temporary string for storing the words by traversing the string with a for loop
- In the case of a space in the answer string, add the current string to the answer and empty the string
- In any other case, push the character into the string
- Reverse the order of printing the answer array
If the string length is 0 when the while loop executes, then cursor terminates execution until the condition i>0 no longer holds.
The print command prints the characters from (i-1 to i>0) of the string.
Step By Step Guide On Reverse Words In A String Java Without Using Functions :-
import java.util.Scanner; class demo { public static void main(String args[]) { String s; Scanner sc=new (System.in); System.out.print("Enter a String: "); s=sc.nextLine(); System.out.print("After reverse string is: "); for(int i=s.length();i>0;--i) { System.out.print(s.charAt(i-1)); } } }
- Defining a Java.util.Scanner import is the first step
- Creating a demo class is the next step.
- Our next step is to define public static void main.
- After that, the string is read from the user
- Our next step is to determine the string's length
- After that, we print the character at index i-1
- In the end, we close the program by
Conclusion :-
For loops are run from index 0 to the length of the set. In the case of charAt(index), each index is inserted into the new string revString using charAt(index).
This method returns all characters to the revString string, which is added to the string returned by charAt().
In the end, the revString string will have the reversed string after the for loop is completed.
I hope this article on reverse words in a string java without using functions helps you and the steps and method mentioned above are easy to follow and implement.