Thursday, 15 August 2013

java - How can I print a String in reverse without local variables one character at a time -



java - How can I print a String in reverse without local variables one character at a time -

i have create recursive method take in string. can not store strings in method.

public static string printbackwards(string s) { if (s.length() == 0) { homecoming s; } else { homecoming printbackwards(s.substring(1)) + s.charat(0); } }

this have far

example input string says " hello " returns terminal o l l e h

if understand question correctly, want method print 1 character @ time. in case homecoming type void.

public static void printbackwards(string s) { if (s.length() != 0) { printbackwards(s.substring(1)); system.out.println(s.charat(0)); } }

java string recursion char

No comments:

Post a Comment