Q:

Java program to Reverse a String

0

Java program to Reverse a String

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Reverse String Example using Java Program

//Java program to Reverse a String.
 
import java.util.*;
 
public class ReverseString
{
    public static void main(String args[]){
        String str;
        String rStr;
        Scanner bf=new Scanner(System.in);
         
        //input an integer number
        System.out.print("Enter any string: ");
        str=bf.nextLine();
         
        //Reversing String
        rStr="";
        for(int loop=str.length()-1; loop>=0; loop--)
            rStr= rStr + str.charAt(loop);
         
        System.out.println("Reversed string is: " + rStr);
    }
}

Output

    me@linux:~$ javac ReverseString.java 
    me@linux:~$ java ReverseString 
    Enter any string: Hello World!
    Reversed string is: !dlroW olleH

Using function/Method

//Java program to Reverse a String.
 
//Using String.ReverseString
import java.util.*;
 
public class ReverseString
{
    public static void main(String args[]){
        String str;
        String rStr="";
        Scanner bf=new Scanner(System.in);
         
        //input an integer number
        System.out.print("Enter any string: ");
        str=bf.nextLine();
         
        //Reversing String
        StringBuffer a = new StringBuffer(str);
        System.out.println(a.reverse());
         
        //rStr=str.reverse();
         
        System.out.println("Reversed string is: " + rStr);
    }
}

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Core Java Example programs for Beginners and Professionals

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Java program to Reverse an Integer Number... >>
<< Java program to print EVEN numbers from 1 to N...