how to print reverse of String "java is object orientated language" without using any predefine function like reverse() in java? any idea.
views:
1353answers:
8First of all: why reinvent the wheel?
That being said: loop from the length of the string to 0 and concatenate into another string.
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));
}
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.
- Obtain the
char[]
of theString
(usingtoCharArray()
) - Iterate backwards over the array inserting each
char
at the end of a newString
(orStringBuilder
)
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));
}
This is the simplest solution:
System.out.print("egaugnal detatneiro tcejbo si avaj");
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:
- What is its stack requirement? Is it prone to stack overflow?
- Is it a tail recursion?
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;
}