Printing reverse of any String without using any predefined function?

后端 未结 30 1311
生来不讨喜
生来不讨喜 2020-11-30 03:08

How to print the reverse of the String java is object orientated language without using any predefined function like reverse()?

相关标签:
30条回答
  • 2020-11-30 03:16

    ReverseString.java

    public class ReverseString {
        public static void main(String[] args) {
            String str = "Ranga Reddy";
            String revStr = reverseStr(str);
            System.out.println(revStr);     
        }
    
        // Way1 - Recursive
        public static String reverseStr(String str) {
            char arrStr[] = reverseString(0, str.toCharArray());
            return new String(arrStr);
        }
    
        private static char[] reverseString(int charIndex, char[] arr) {     
            if (charIndex > arr.length - (charIndex+1)) {
              return arr;
            }
    
            int startIndex = charIndex;
            int endIndex = arr.length - (charIndex+1);
    
            char temp = arr[startIndex];        
            arr[startIndex] = arr[endIndex];
            arr[endIndex] = temp;      
            charIndex++;       
            return reverseString(charIndex++, arr);
        }
    
        // Way2
        private static String strReverse(String str) {
            char ch[] = new char[str.length()];
            for (int i = str.length() - 1, j = 0; i >= 0; i--) {
                ch[j++] = str.charAt(i);
            }
            return new String(ch);
        }
    }
    
    0 讨论(0)
  • 2020-11-30 03:16

    It is very simple using while loop

    public class Test {
    public static void main(String[] args) {
        String name = "subha chandra";
        int len = name.length();
        while(len > 0){
            len--;
            char c = name.charAt(len);
            System.out.print(c); // use String.valueOf(c) to convert char to String
        }
    }    
    }
    
    0 讨论(0)
  • 2020-11-30 03:19
    final String s = "123456789";
    final char[] word = s.toCharArray();
    final int l = s.length() - 2;
    final int ll = s.length() - 1;
    for (int i = 0; i < l; i++) {
        char x = word[i];
        word[i] = word[ll - i];
        word[ll - i] = x;
    }
    System.out.println(s);
    System.out.println(new String(word));
    

    You can do it either recursively or iteratively (looping).

    Iteratively:

    static String reverseMe(String s) {
        StringBuilder sb = new StringBuilder();
        for (int i = s.length() - 1; i >= 0; --i)
            sb.append(s.charAt(i));
        return sb.toString();
    }
    

    Recursively:

    static String reverseMe(String s) {
        if (s.length() == 0)
            return "";
        return s.charAt(s.length() - 1) + reverseMe(s.substring(1));
    }
    

    Integer i = new Integer(15);
    test(i);
    System.out.println(i);
    test(i);
    System.out.println(i); 
    public static void test (Integer i) {
        i = (Integer)i + 10;
    }
    
    0 讨论(0)
  • 2020-11-30 03:20
    String a="Siva";
    
    for(int i=0;i<=a.length()-1;i++){
        System.out.print(a.charAt(i));
    }
    
    for(int i = a.length() - 1; i >= 0; --i){
        System.out.println(a.charAt(i)); 
    }
    
    0 讨论(0)
  • 2020-11-30 03:20
    private void rev() {
        String st="hello";
        String b="";
    
        for(int i=st.length()-1;i>=0;i--){
            b=b+st.charAt(i);
        }
    
        System.out.println("reverse:::"+b);
    }
    
    0 讨论(0)
  • 2020-11-30 03:20
    public class ReverseString {
    
    public static void main(String [] args) {
    
        String s = "reverse string" ;
        String b = "";
    
                for (int i = 0; i < s.length(); i++ ){
                     b= b + s.substring(s.length()-1-i, s.length()-i);
    
                     }
    
                 System.out.println(b);
    }
    
    0 讨论(0)
提交回复
热议问题