COMP2247 - Algorithms and Data Structures

Syllabus Software Installation Assignments Tests
Lectures and Labs
**Back to Full Menu**

Recursion Factorial Program Fibonacci Program Integer Power Program Tower of Hanoi Program Max Element Program Palindrome Program Reverse String Program Array Sum Program



Palindrome Program


Description

The program inputs a string and uses a recursive method to determine whether or not it is a palindrome.

For example, "Race car" and "Step on no pets!" are palindromes.

The program should remove blank spaces and punctuations from the input string.

Associated Concepts:

Recursion

Algorithm:

main method:

Input a string

Convert the input string to uppercase letters

Remove the blank spaces and punctuations in the input string

Call isPalindrome method with the input string as the argument and use a boolean variable flag to hold the returned value

If flag is true

    Yes

Otherwise

    No

isPalindrome method definition:

If the input string has only one character or is empty, return true.

Otherwise, compare the first element and the last element.

If they are equal, call isPalindrome method recursively on the substring from the second element to the second to the last element.

Otherwise, return false.

Source Code



Sample Run



Video