Find us on Google+ Kill the code: Reverse String Dramatically in JAVA

Sunday, 18 November 2012

Reverse String Dramatically in JAVA

Input    : my name is nisarg mehta
Output : mehta nisarg is name my


public class reverse_string {
    public static void main(String args[]) {
        String s = "my name is nisarg mehta";
        String output = "";
        int space = 4;
        String temp = "";
        int i = s.length();
        int j = i;
        while(space >= 0) {
            while(i>=1 && s.charAt(--i) != ' ') {
            }
            temp = s.substring(i,j);
            j = i;
            output += " " + temp;
            temp = "";
            space--;
        }
        System.out.println("Case#1 : "+output);
    }
}

No comments:

Post a Comment