Java - printing one character at a time using recursion -
i'm trying print 1 character @ time string recursively such as:
va ava java java o java lo java llo java ello java hello java
but code below:
public static string displaystuffr(string x){ return displaystuffr(0,x); } public static string displaystuffr(int i,string x){ if (i<x.length()){ return x.substring(x.length()-1-i) + displaystuffr(i+1,x);} return x; } public static void main(string args[]){ system.out.print(displaystuffr("hello java")); }
displays:
avaavajava javao javalo javallo javaello javahello javahello java
much appreciated
when return string @ time use new line string returned in display function
return x.substring(x.length()-1-i)+"\n" + displaystuffr(i+1,x);
and update if condition
i < x.length()-1
Comments
Post a Comment