java - Palindrome Program using Recursion -
this have palindrome program computer science class. have pretty working, except whenever word palindrome, infinite loop. know have insert number base case, not how that...i'm having trouble understanding recursion. appreciated.
public class palindrometester { public static void main(string[] args) { scanner scan = new scanner (system.in); string str, = "y"; int left, right; while (another.equalsignorecase("y")) { system.out.println("enter potential palindrome:"); str = scan.next(); left = 0; right = str.length() - 1; tester(str, left, right); system.out.println(); system.out.println("test palindrome (y/n)?"); = scan.next(); } } public static void tester (string str, int left, int right) { scanner scan = new scanner (system.in); while (str.charat(left) == str.charat(right) && left < right) { system.out.println(str); tester( str, left + 1, right -1); } if (left < right) { system.out.println("that string not palindrome."); } else { system.out.println("that string palindrome."); } } }
you using while loop. recursion, done implicitly.
you have split algorithm in small parts.
[] represents left, {} represents right.
[1] 2 3 4 5 6 7 8 9 0 9 8 7 6 5 4 3 2 {1} -->level 0 1 [2] 3 4 5 6 7 8 9 0 9 8 7 6 5 4 3 {2} 1 -->level 1 1 2 [3] 4 5 6 7 8 9 0 9 8 7 6 5 4 {3} 2 1 -->level 2 1 2 3 [4] 5 6 7 8 9 0 9 8 7 6 5 {4} 3 2 1 -->level 3 1 2 3 4 [5] 6 7 8 9 0 9 8 7 6 {5} 4 3 2 1 -->level 4 1 2 3 4 5 [6] 7 8 9 0 9 8 7 {6} 5 4 3 2 1 -->level 5 1 2 3 4 5 6 [7] 8 9 0 9 8 {7} 6 5 4 3 2 1 -->level 6 1 2 3 4 5 6 7 [8] 9 0 9 {8} 7 6 5 4 3 2 1 -->level 7 1 2 3 4 5 6 7 8 [9] 0 {9} 8 7 6 5 4 3 2 1 -->level 8 1 2 3 4 5 6 7 8 9 {[0]} 9 8 7 6 5 4 3 2 1 -->level 9
so, tester
continue until:
- we've reached middle of word.
- the word not palindrome
example of case 2:
[1] 2 3 5 6 7 8 9 0 9 8 7 6 5 4 3 2 {1} 1 [2] 3 5 6 7 8 9 0 9 8 7 6 5 4 3 {2} 1 1 2 [3] 5 6 7 8 9 0 9 8 7 6 5 4 {3} 2 1 1 2 3 [a] 5 6 7 8 9 0 9 8 7 6 5 {4} 3 2 1 --> !!!
i thought method helpful understanding of how recursion working
public static string positions(string word, int l, int r) { char[] = word.tochararray(); string s = ""; // [letter] if left, {} if right, [{}] if both (int = 0; < a.length; i++) { if (l == && r == i) { s += "{[" + a[i] + "]}"; } else if (l == i) { s += "[" + a[i] + "]"; } else if (r == i) { s += "{" + a[i] + "}"; } else { s += a[i]; } s+=" "; } return s; }
and finally, tester
method.
public static boolean tester(string str, int left, int right) { system.out.println(positions(str, left, right) +" tester(str, "+left +", "+right+")"); if (left>=right) // case 1 return true; // that's ok, we've reached middle // middle not reached yet. // condition satisfied? if (str.charat(left) == str.charat(right)) { // yes. so, lets again, parameters changed return tester(str, left + 1, right - 1); } //the condition not satisfied. let's out of here. else { return false; } }
some outputs:
enter potential palindrome: 1234567890987654321 [1] 2 3 4 5 6 7 8 9 0 9 8 7 6 5 4 3 2 {1} tester(str, 0, 18) 1 [2] 3 4 5 6 7 8 9 0 9 8 7 6 5 4 3 {2} 1 tester(str, 1, 17) 1 2 [3] 4 5 6 7 8 9 0 9 8 7 6 5 4 {3} 2 1 tester(str, 2, 16) 1 2 3 [4] 5 6 7 8 9 0 9 8 7 6 5 {4} 3 2 1 tester(str, 3, 15) 1 2 3 4 [5] 6 7 8 9 0 9 8 7 6 {5} 4 3 2 1 tester(str, 4, 14) 1 2 3 4 5 [6] 7 8 9 0 9 8 7 {6} 5 4 3 2 1 tester(str, 5, 13) 1 2 3 4 5 6 [7] 8 9 0 9 8 {7} 6 5 4 3 2 1 tester(str, 6, 12) 1 2 3 4 5 6 7 [8] 9 0 9 {8} 7 6 5 4 3 2 1 tester(str, 7, 11) 1 2 3 4 5 6 7 8 [9] 0 {9} 8 7 6 5 4 3 2 1 tester(str, 8, 10) 1 2 3 4 5 6 7 8 9 {[0]} 9 8 7 6 5 4 3 2 1 tester(str, 9, 9) true test palindrome (y/n)? y enter potential palindrome: 12345a678654321 [1] 2 3 4 5 6 7 8 6 5 4 3 2 {1} tester(str, 0, 14) 1 [2] 3 4 5 6 7 8 6 5 4 3 {2} 1 tester(str, 1, 13) 1 2 [3] 4 5 6 7 8 6 5 4 {3} 2 1 tester(str, 2, 12) 1 2 3 [4] 5 6 7 8 6 5 {4} 3 2 1 tester(str, 3, 11) 1 2 3 4 [5] 6 7 8 6 {5} 4 3 2 1 tester(str, 4, 10) 1 2 3 4 5 [a] 6 7 8 {6} 5 4 3 2 1 tester(str, 5, 9) false test palindrome (y/n)?
in main
method,
system.out.println(tester(str, left, right));
in order see true/false
output
Comments
Post a Comment