What's the purpose of pal[index] = 30; ? You're changing the first character, thus making a test always fail?
Two things missing from your logic:
- The initial test of length == 0 only works on first call. Subsequent recursive calls must compare your shortened "word" length, which really should indicate index of character being compared, not actual string length. So, something like length-index== 0 is what you need.
- When making the recursive call, you must move in both the starting and ending points (index and length), and you must use preincrement/predecrement operators. Using post increment, as you do for index++, send the current value to the next call, not the update that you want to send.
Chococrack - your solution is iterative, while the OP seems to want/need a recursive solution. That said, your loop does twice the work it should, try while ( index < length/2 ) , and your line 13 has the infamous OBO error.
if (pal[index] != pal[length - index -1]) solves that - remember that length is count of items, last valid item in a zero-based counting is length-1.
Reputation Points: 1268
Solved Threads: 228
Posting Virtuoso
Offline 1,895 posts
since Aug 2007