-->

9/19/2019

Write a Java Program to reverse a string without using String inbuilt function reverse().

Method 1:

1public class FinalReverseWithoutUsingInbuiltFunction {
2    public static void main(String[] args) {
3        String str = "Saket Saurav";
4        char chars[] = str.toCharArray();  // converted to character array and printed in reverse order
5        for(int i= chars.length-1; i>=0; i--) {
6            System.out.print(chars[i]);
7        }
8    }
9}
Output:

varuaS tekaS

Method 2:
1import java.util.Scanner;
2 
3public class ReverseSplit {
4 
5    public static void main(String[] args) {
6        // TODO Auto-generated method stub
7        String str;
8        Scanner in = new Scanner(System.in);
9        System.out.println("Enter your String");
10        str = in.nextLine();
11        String[] token = str.split("");    //used split method to print in reverse order
12        for(int i=token.length-1; i>=0; i--)
13        {
14            System.out.print(token[i] + "");
15        }
16         
17    }
18 
19}
Output:
Enter your String
Softwaretestinghelp
plehgnitseterawtfoS
NEXT ARTICLE Next Post
PREVIOUS ARTICLE Previous Post
NEXT ARTICLE Next Post
PREVIOUS ARTICLE Previous Post