QBASIC program to input any string and check whether the input string is palindrome or not
A palindrome is a string that is the same when read from left to right or right to left. For example, "racecar" is a palindrome, as it is the same when read from left to right or right to left.
Algorithm
The algorithm for checking whether a string is a palindrome or not is simple. Here are the steps:
- Accept a string as input from the user.
- Initialize two variables, one at the beginning of the string and the other at the end of the string.
- Compare the characters at the beginning and end of the string.
- If they are the same, move both the variables one character to the right and left, respectively, and continue the comparison.
- If they are not the same, the string is not a palindrome.
QBASIC Program to Check Palindrome
CLS
INPUT
"Enter any string:"; wo$
FOR
x = LEN(wo$) TO 1 STEP -1
r$
= r$ + MID$(wo$, x, 1)
NEXT
x
IF
r$ = wo$ THEN
PRINT
wo$; " is a palindrome"
ELSE
PRINT
wo$; " is not palindrome"
END
IF
END
By
using SUB procedure (SUB…END SUB)
DECLARE SUB rev (wo$)
CLS
INPUT
"Enter any string:"; wo$
CALL
rev(wo$)
END
SUB
rev (wo$)
FOR
x = LEN(wo$) TO 1 STEP -1
r$
= r$ + MID$(wo$, x, 1)
NEXT
x
IF
wo$ = r$ THEN
PRINT
wo$; " is palindrome"
ELSE
PRINT
wo$; " is not palindrome"
END
IF
END
SUB
By using the FUNCTION procedure (FUNCTION..END FUNCTION)
DECLARE FUNCTION rev$ (wo$)
CLS
INPUT
"Enter any string:"; wo$
rv$
= rev$(wo$)
PRINT
wo$; rv$
END
FUNCTION
rev$ (wo$)
FOR
x = LEN(wo$) TO 1 STEP -1
r$
= r$ + MID$(wo$, x, 1)
NEXT
x
IF
wo$ = r$ THEN
rev$
= " is palindrome string"
ELSE
rev$
= " is not palindrome string"
END
IF
END
FUNCTION
Output:
Conclusion
No comments:
Post a Comment