pTutorial

New way to learn Programming

Java reverse string example

Following java program reverse the string with the help of toCharArray() method of string class that is basically used to convert the string into the character type array. You can easily make this program user define by using scanner class as previous describe.

For example

Given string is: Java
Result is: avaJ

Java reverse string Example

class ReverseString{
    
static String str;

static String revers(String v){
    
    str=v;
    
    int j=0;
    
    char ch[] =str.toCharArray();
    
    char[] ch1=new char[ch.length];
    
    for(int i=ch.length-1;i>=0;i--,j++){
        
    ch1[j]=ch[i];
    
    }
    
    String temp=new String(ch1);
    
    return temp;
    }
    public static void main(String...s){
        
        String str1;
        
        str1 = revers("Reverse String in java");
        
        System.out.println(str1);
        
        str1 = revers("java");
        
        System.out.println(str1);
    }
}

Output

avaj ni gnirtS esreveR
avaj

See Also


QUOTES OF THE DAY

The basis of all progress is self-reliance

LIKE ON FACEBOOK

+1 ON GOOGLE

SHARE ON TWITTER