Reverse a string in Java

前端 未结 30 3088
礼貌的吻别
礼貌的吻别 2020-11-21 13:12

I have \"Hello World\" kept in a String variable named hi.

I need to print it, but reversed.

How can I do this? I understand there

30条回答
  •  滥情空心
    2020-11-21 13:47

    All above solution is too good but here I am making reverse string using recursive programming.

    This is helpful for who is looking recursive way of doing reverse string.

    public class ReversString {
    
    public static void main(String args[]) {
        char s[] = "Dhiral Pandya".toCharArray();
        String r = new String(reverse(0, s));
        System.out.println(r);
    }
    
    public static char[] reverse(int i, char source[]) {
    
        if (source.length / 2 == i) {
            return source;
        }
    
        char t = source[i];
        source[i] = source[source.length - 1 - i];
        source[source.length - 1 - i] = t;
    
        i++;
        return reverse(i, source);
    
    }
    
    }
    

提交回复
热议问题