Reverse string program in java without using function

Reverse string program in java: To reverse a string in Java without using a built-in function, you can implement the following algorithm:

  1. Create an empty string variable to store the reversed string.
  2. Loop through the original string in reverse order, starting from the last character and ending with the first character.
  3. For each character in the original string, append it to the empty string variable created in step 1.
  4. Once the loop is complete, the empty string variable will contain the reversed string.

Here is an example implementation of the above algorithm in Java:

Ex.1 Reverse string program in java

public class A{
	public static void main(String[] args) {
	String str ="Rajendra Rathor";
	for (int i = str.length()-1; i >0; i--) {
		System.out.print(str.charAt(i));
	}
}
}
Output: 
rohtaR ardneja

Ex. Reverse string program in java without using function

public class ReverseString {
    public static void main(String[] args) {
        String originalString = "hello world";
        String reversedString = "";

        for (int i = originalString.length() - 1; i >= 0; i--) {
            reversedString += originalString.charAt(i);
        }

        System.out.println("Original string: " + originalString);
        System.out.println("Reversed string: " + reversedString);
    }
}

To reverse a string in Java without using a built-in function, you can implement the following algorithm:

  1. Create an empty string variable to store the reversed string.
  2. Loop through the original string in reverse order, starting from the last character and ending at the first character.
  3. For each character in the original string, append it to the empty string variable created in step 1.
  4. Once the loop is complete, the empty string variable will contain the reversed string.

Here is an example implementation of the above algorithm in Java:

typescriptCopy codepublic class ReverseString {
    public static void main(String[] args) {
        String originalString = "hello world";
        String reversedString = "";

        for (int i = originalString.length() - 1; i >= 0; i--) {
            reversedString += originalString.charAt(i);
        }

        System.out.println("Original string: " + originalString);
        System.out.println("Reversed string: " + reversedString);
    }
}

In the above code, we first create a string variable originalString to store the input string. We then create an empty string variable reversedString to store the reversed string.

We then loop through the original string in reverse order using a for loop. For each character in the original string, we append it to the reversedString variable using the += operator.

Once the loop is complete, the reversedString variable will contain the reversed string. We then print both the original and reversed strings using the System.out.println() function.

Note that this implementation creates a new string variable for each character in the original string, which can be inefficient for very long strings. In practice, it is more efficient to use a StringBuilder or StringBuffer object to build the reversed string character by character, since these objects can dynamically allocate memory as needed.

2 thoughts on “Reverse string program in java without using function”

Leave a Comment