COMP2243 - Programming and Problem Solving

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

Methods RightTriangle.java RightTriangleWithLoop.java Geometry.java PayrollWithMethod.java PayrollWithMethodLoop.java Palindrome.java GlobalAndLocal.java Overloading.java Overloading2.java MathMethods.java and MathProgram.java CaesarCipher.java



Palindrome Program


Description

The program inputs a string and uses a 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:

Method

String methods

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 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:

Set i to the left most index of the input string

Set j to the right most index of the input string

while loop (as long as i is less than j):

    Compare the characters at i and j

    If they are equal, continue the process

    Otherwise

        Stop the process by returning false

In the end, if the while loop executed successfully in its entirety without returning false, we know that the input string is a palindrome. So the value of true is returned.

Source Code



Sample Run



Video