Reverse String in Java and Android


Are you getting problem during reverse string in java or android, here is the simple solution fro reverse string.

First things, Just define this string:

        String tempString = "AndroidandJavaTutorials";
        String reverseString = new StringBuffer(tempString).reverse().toString();
        System.out.printf(" original String -> %s , reversed String %s  %n", tempString, reverseString);
   

        tempString = "pRAndroid";
        reverseString = new StringBuilder(tempString).reverse().toString();
        System.out.printf(" original String- > %s , reversed String %s %n", tempString, reverseString);

Now, Call the funtion, where exactly process of reserve:

public static String reverse(String source){
        if(source == null || source.isEmpty()){
            return source;
        }    
        String reverse = "";
        for(int i = source.length() -1; i>=0; i--){
            reverse = reverse + source.charAt(i);
        }
   
        return reverse;
    }

Final output of above tempString:

 original String -> AndroidandJavaTutorials , reversed String slairotuTavaJdnadiordnA  
 original String- > pRAndroid , reversed String diordnARp 

Happy Coding!!!
Reverse String in Java and Android Reverse String in Java and Android Reviewed by D Kshetri on 12:58 AM Rating: 5

No comments:

Powered by Blogger.