161k views
4 votes
Write a recursive method to reverse a string. explain why you would not normally use recursion to solve this problem

User Rashkay
by
8.3k points

1 Answer

3 votes
Here's a Python program:

#!/usr/bin/python

import sys

def isPalindrome( string ):
if( len( string ) < 2 ):
return True
elif( string[ 0 ] == string [ -1 ] ):
return( isPalindrome( string[ 1: -1 ] ) )
else:
return False

if( __name__ == "__main__" ):
print isPalindrome( sys.argv[ 1 ] )


User Bbrinx
by
8.1k points