Home » How to Reserve a String in Java without Using Reverse Function

How to Reserve a String in Java without Using Reverse Function

by Online Tutorials Library

How to reserve a string in Java without using reverse function

There are following ways to reverse a string in Java:

  • Using for loop
  • Using While loop
  • Using static method

Using For loop

Example to reverse string in Java by using for loop

In the following example, we have used for loop to reverse a string. The for loop executes until the condition i>0 becomes false.

Output:

How to reserve a string in Java without using reverse function


Using while loop

Example to reverse string in Java by using while loop

In the following example, i is the length of the string. The while loop execute until the condition i>0 becomes false i.e. if the length of the string is 0 then cursor terminate the execution. It prints the characters of the string which is at index (i-1) until i>0.

Output:

How to reserve a string in Java without using reverse function


Using Static method

Example to reverse string in Java by using static method

In the following example, we have created an object of the class and called the static method with the object as rev.reverse(s) by passing the given string.

Output:

How to reserve a string in Java without using reverse function


Next TopicJava Tutorial

You may also like