All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Reverse Words In A String Java Without Using Functions

Last Updated : Mar 11, 2024

Reverse Words In A String Java Without Using Functions

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));
}
}
}
  1. Defining a Java.util.Scanner import is the first step
  2. Creating a demo class is the next step.
  3. Our next step is to define public static void main.
  4. After that, the string is read from the user
  5. Our next step is to determine the string's length
  6. After that, we print the character at index i-1
  7. 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.

Author Image About Pragati

Experienced coding content writer who enjoys breaking down complex concepts in programming languages like Java, Python, C, and C++. Over three years of experience producing interesting and relevant content for a variety of entities. Committed to providing concise and easy-to-understand articles that assist readers in easily understanding technology and industry trends in the world of coding and software development.

Follow Pragati On Linkedin 🡪