To Check The Word Palindrome Or Not (Qbasic)

When user inputs the word using same case only (i.e.uppercase or lowercase)
CLS
 INPUT "Enter a word:"; w$
 FOR i = LEN(w$) TO 1 STEP -1
     m$ = MID$(w$, i, 1)
     rev$ = rev$ + m$
 NEXT i
 PRINT
 PRINT "The original word is "; w$
 PRINT "The reverse wod is "; rev$
 PRINT
 IF w$ = rev$ THEN
     PRINT "The given word is palindrome"
 ELSE
     PRINT "The given word is not palindrome"
 END IF
 END


When user inputs the word using uppercase and lowercase letters.
 CLS
 INPUT "Enter a word: "; w$
 FOR i = LEN(w$) TO 1 STEP -1
     rev$ = rev$ + MID$(w$, i, 1)
 NEXT i
 PRINT
 PRINT "The original word is "; LCASE$(w$)
 PRINT "The reverse word is "; LCASE$(rev$)
 PRINT
 IF LCASE$(w$) = LCASE$(rev$) THEN
     PRINT "The word is a palindrome"
 ELSE
     PRINT "The word is not a palindrome"
 END IF
 END


Using SUB procedure 
DECLAREe sub palindrome(w$)
CLS
INPUT "Enter a word"; w$
CALL palindrome(w$)
END

SUB palindrome (w$)
FOR i = LEN(w$) TO 1 STEP -1
    rev$ = rev$ + MID$(w$, i, 1)
NEXT i
IF rev$ = w$ THEN
    PRINT "The given word is palindrome"
ELSE
    PRINT "The given word is not palindrome"
END IF
END SUB


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

Share :

Twitter
Back To Top

facebook main

counter

Powered by Blogger.