Question 5
A palindrome is a word which can be written in reversed order and it retains its meaning and spelling. Examples are Lawal, Ada, Abba, etc.
Write a simple QBASIC instruction that will accept word from the keyboard and check if it is a Palindrome or not.
The program must display a message showing if the word is a Palindrome or not.
Observation
The expected answer is:
Question five
QBASIC Program to accept palindrome
10 CLS
20 DIM Enword AS STRING
30 DIM Revword AS STRING
40 INPUT “Enter a word:”; Enword
50 FOR i = LEN(Enword) TO 1 STEP -1
60 Revword = Revword + MID$ (Enword, i/ 1)
70 NEXT i
80 PRINT
90 IF LCASE$ (Enword) = LCASE$ (Revword) THEN
100 PRINT “This is a palindrome:” Enword;
110 “Reversed word:” Revword
120 ELSE
130 PRINT “This is not a palindrome:”
140 Enword; “Reversed word:”; Revword
150 ENDIF
160 END
OR
CLS
10 INPUT "ENTER A WORD:", W$
20 X = LEN(W$)
30 FOR i = X TO 1 STEP -1
40 wrd$ = wrd$ + MID$(W$, i, 1)
50 NEXT i
60 PRINT
70 PRINT "The original word is:"; LCASE$(W$)
80 PRINT "The reverse word is"; LCASE$(wrd$)
90 PRINT
100 IF LCASE$(W$) = LCASE$(wrd$) THEN
120 PRINT "It is Palindrome"
130 ELSE
140 PRINT "Not Palindrome"
150 END IF
160 END
OR
CLS
INPUT "ENTER A WORD:", W$
X = LEN(W$)
FOR i = X TO 1 STEP -1
m$ = MID$(W$, i, 1)
wrd$ = wrd$ + m$
NEXT
PRINT
PRINT "The original word is:"; W$
PRINT "The reverse word is"; wrd$
PRINT
IF W$ = wrd$ THEN
PRINT "It is Palindrome"
ELSE
PRINT "Not Palindrome"
END IF
END
OR
Using SUB procedure
DECLARE Sub palin(w$)
CLS
INPUT "ENTER A WORD:", W$
callpalin(w$)
END
Sub palin(w$)
X = LEN(W$)
FOR i = X TO 1 STEP -1
rev$ = rev$ + MID$(W$, i, 1)
NEXT i
IF rev$ = w$ THEN
PRINT "It is Palindrome"
ELSE
PRINT "Not Palindrome"
END IF
END SUB
OR
Using FUNCTION procedure
DECLARE FUNCTION palindrome$ (w$)
CLS
INPUT "Enter any word"; w$
a$ = w$
IF a$ = palindrome$(w$) THEN
PRINT w$; " is palindrome word";
ELSE
PRINT w$; " is not palindrome word";
END IF
FUNCTION palindrome$ (w$)
FOR i = LEN(w$) TO 1 STEP -1
rev$ = rev$ + MID$(w$, i, 1)
NEXT i
palindrome$ = rev$
END FUNCTION