Sunday, March 31, 2013

QBasic - Checking Palindrome Word

Description:
This tutorial will show how find if a word or string is a palindrome. A palindrome is a word or string that is the same printed forwards and backward.

Examples:
bob
noon
1991
redder

Note: the Oxford English Dictionary states that the longest palindromic word is tattarrattat.

The Guinness Book of Records states that detartrated is the longest word and a soap dish vender saippuakuppinippukauppias is a long palindrome name. (Don’t ask how to pronounce that name).


Note: The program uses LCASE$ instead of UCASE$. LCASE$ converts strings to lowercase.

Code:
DIM Wrd AS STRING
DIM RevWrd AS STRING
DIM x AS INTEGER

CLS

INPUT "Enter Word: ", Wrd

FOR x = LEN(Wrd) TO 1 STEP -1
    RevWrd = RevWrd + MID$(Wrd, x, 1)
NEXT x

PRINT
PRINT "Original Word: "; LCASE$(Wrd)
PRINT "Reverse Word: "; LCASE$(RevWrd)

IF LCASE$(Wrd) = LCASE$(RevWrd) THEN
    PRINT "The Word Is A Palindrome"
ELSE
    PRINT "The Word Is Not A Palindrome"
END IF

0 comments:

Post a Comment