views:

1353

answers:

8

how to print reverse of String "java is object orientated language" without using any predefine function like reverse() in java? any idea.

+2  A: 

First of all: why reinvent the wheel?

That being said: loop from the length of the string to 0 and concatenate into another string.

Paulo Santos
+2  A: 

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));
 }
Itay
-1 for just posting a copy and paste solution for such an obvious homework assignment.
Bart Kiers
To downvoters: please read the guidelines: http://meta.stackoverflow.com/questions/10811/how-to-ask-and-answer-homework-questions : "Don't downvote others who answer homework questions in good faith, even if they break these guidelines."
polygenelubricants
@polygenelubricants looks like most agree that this is an obvious homework question and the guidelines talk about non obvious ones.
josefx
+3  A: 

Well, printing itself would suggest a predefined function...

Presumably, though, you could obtain the characters and concatenate them manually in reverse (i.e. loop over it backwards). Of course, you could say concatenation is a predefined function... so maybe the char array itself. But again... why?

Is the source allowed to contain "egaugnal detatneiro tcejbo si avaj" ;-p

Also - note that string reversal is actually pretty complex if you consider unicode combining characters, surrogate pairs, etc. You should note the caveat that most string reversal mechanisms will only deal with the more common cases, but may struggle with i18n.

Marc Gravell
A: 
  1. Obtain the char[] of the String (using toCharArray())
  2. Iterate backwards over the array inserting each char at the end of a new String (or StringBuilder)
Bozho
+1  A: 

How about a simple traverse from the end of the string to the beg:

void printRev(String str) {
 for(int i=str.length()-1;i>=0;i--)
  System.out.print(str.charAt(i));
}
codaddict
+3  A: 

This is the simplest solution:

System.out.print("egaugnal detatneiro tcejbo si avaj");
Adriaan Koster
A: 

Here's a recursive solution that just prints the string in reverse order. It should be educational if you're trying to learn recursion. I've also made it "wrong" by actually having 2 print statements; one of them should be commented out. Try to figure out which mentally, or just run experiments. Either way, learn from it.

static void printReverse(String s) {
    if (!s.isEmpty()) {
        System.out.print(s.substring(0, 1));
        printReverse(s.substring(1));
        System.out.print(s.substring(0, 1));
    }
}

Bonus points if you answer these questions:

polygenelubricants
A: 

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;
   }
jeet