QBASIC Program to input any string from the user and display whether that string is palindrome or not.

5 months ago

A palindrome number is a number that remains the same when its digits are reversed. In other words, if you read a palindrome number from left to right or right to left, it will still be the same number. Palindrome numbers are symmetrical.

For example:

121 is a palindrome because if you reverse its digits, you still get 121.
1331 is a palindrome for the same reason.
1221 is a palindrome.

However, numbers like 123 and 456 are not palindromes because they are not the same when read backward.

In the context of a palindrome string, it follows the same principle but with characters of a string. If a string remains the same when its characters are reversed, it is a palindrome string. Examples include "radar," "level," and "madam."

Here's a simple QBASIC program to check if a given string is a palindrome:

CLS
INPUT "Enter a string: ", str
originalStr = UCASE$(str) 
length = LEN(originalStr)
isPalindrome = 1

FOR i = 1 TO length \ 2
    IF MID$(originalStr, i, 1) <> MID$(originalStr, length - i + 1, 1) THEN
        isPalindrome = 0
        EXIT FOR
    END IF
NEXT i

IF isPalindrome THEN
    PRINT str; " is a palindrome."
ELSE
    PRINT str; " is not a palindrome."
END IF
END

 

Using SUB Procedure

DECLARE SUB CheckPalindrome(inputStr)

CLS
INPUT "Enter a string: ", userString
CALL CheckPalindrome(userString)

SUB CheckPalindrome(inputStr)
    originalStr = UCASE$(inputStr) 
    length = LEN(originalStr)
    isPalindrome = 1

    FOR i = 1 TO length \ 2
        IF MID$(originalStr, i, 1) <> MID$(originalStr, length - i + 1, 1) THEN
            isPalindrome = 0
            EXIT FOR
        END IF
    NEXT i

    IF isPalindrome THEN
        PRINT inputStr; " is a palindrome."
    ELSE
        PRINT inputStr; " is not a palindrome."
    END IF
END SUB

END

 

Using FUNCTION Procedure

DECLARE FUNCTION IsPalindrome(inputStr)
CLS
INPUT "Enter a string: ", str
result = IsPalindrome(str)

IF result THEN
    PRINT str; " is a palindrome."
ELSE
    PRINT str; " is not a palindrome."
END IF

FUNCTION IsPalindrome(inputStr)
    originalStr = UCASE$(inputStr)
    length = LEN(originalStr)
    isPalindrome = 1

    FOR i = 1 TO length \ 2
        IF MID$(originalStr, i, 1) <> MID$(originalStr, length - i + 1, 1) THEN
            isPalindrome = 0
            EXIT FOR
        END IF
    NEXT i

    RETURN isPalindrome
END FUNCTION
END

  466