How to Write a Program to Check Whether a String Is a Palindrome or Not

Palindromes are words that have the same characters both forwards and reverse. They are not case-sensitive and do not take punctuation and white space into consideration, so remember to remove these characters from the strings before testing the string. "Radar" and "race car" are both examples of palindromes. You can write a function to check for palindromes in any programming language, such as C++, Visual Basic or PHP, but the syntax for each language and functions you need differ in each.

Instructions

  1. C++

    • 1

      Open a C++ program file. Insert the cursor at the top of the file.

    • 2

      Type the following code:

      include <iostream>
      include <string>

      using namespace std;

      These lines include the necessary header files and set the namespace to use.

    • 3

      Insert the cursor where you want to check for palindromes. Type the following code:

      string word;

      bool palindrome;

      cout << "Enter a string: ";

      cin >> word;

      The first line creates a string variable. The second creates a Boolean variable that holds the value of whether the string is a palindrome or not. The third line prompts the user to type in a string and the fourth saves it to the string variable.

    • 4

      Type the following code:

      for (int x=0; x < word.length()-1; x++) {

      if (word[x] != ' ') {

      if (tolower(word[x]) != tolower(word[word.length()-(x+1)])) {

      palindrome = false;

      break;

      }

      else { palindrome = true; }

      }

      }

      The for loop starts checking the string. The first if statement checks to see if the character is a space. If so, it skips it. The next if statement converts the string to lowercase and checks it against the reversed string, character by character. If any of the characters do not match, the function breaks because the string is not a palindrome. Otherwise, it is a palindrome and the Boolean variable is set to true.

    • 5

      Type the following code:

      if (palindrome) cout << "The string is a palindrome";

      else cout << "The string is not a palindrome";

      These lines check the palindrome variable and if it is true, it informs the user the string is a palindrome. If not, it tells the user it is not.

    Visual Basic

    • 6

      Open a new Visual Basic program. Double-click the Label, TextBox and Button controls to add one of each to the form. Arrange them as you would like.

    • 7

      Double-click the "Button1" control to open the "Button1_Click" subroutine. Type the following code:

      Dim forward As String = UCase(TextBox1.Text)

      forward = Replace(forward, " ", "")

      Dim backward As String = StrReverse(forward)

      The first line creates a string variable. It gets the message that the user types into the text box and converts the characters to uppercase. The second line removes all spaces in the string. The third line makes a copy of the original string, but reverses the characters.

    • 8

      Type the following code:

      If forward = backward Then

      Label1.Text = "The string is a palindrome."

      Else

      Label1.Text = "The string is not a palindrome."

      End If

      The if statement checks to see if the two strings are equal. If they are, the original string the user typed is a palindrome. If they are not equal, the string is not a palindrome.

    PHP

    • 9

      Open an HTML file. Insert the cursor where you want to check for palindromes.

    • 10

      Type the following code:

      <form action="" method="post">

      Type a word: <input type="text" name="word">

      <input type="submit" name="submit" value="Check">

      </form>

      This creates a form that prompts the user to enter a string into a text box. When the user clicks the submit button, the form passes the string to the PHP function.

    • 11

      Type the following code:

      <?php

      if (isset($_POST['word'])) {

      $word = strtolower( $_POST['word'] );

      $patterns = array ('/ /', '/[^\sa-z0-9]/');

      $word = preg_replace($patterns, '', $word );

      if($word == strrev($word)) { echo 'The string is a palindrome.';

      } else { echo 'The string is not a palindrome.'; }

      }

      ?>

      The first line opens a PHP tag. The second checks to see if the user has submitted a string, and if so, it gets the string, converts all of the characters to lowercase and saves it to a variable. The next line creates an array with patterns to replace within the string, such as punctuation and white spaces. The function then uses the "preg_replace" function to remove those extra characters from the string. Finally, the function checks to see if the string is equal to the reverse of the string and if so, informs the user that the string is a palindrome and if not, tells the user the string is not.

Related Searches

References

You May Also Like

Related Ads

Featured