Computer for SEE and NEB

It is a complete SEE and NEB solution for computer science. It includes Computer Fundamentals, Database (SQL), Programming in C QBASIC, CSS, JavaScript, and PHP for beginners.

Breaking

Post Top Ad

Your Ad Spot

Friday, December 9, 2022

QBASIC program to input any string and check whether the input string is palindrome or not

 QBASIC program to input any string and check whether the input string is palindrome or not

 

QBASIC program to input any string and check whether the input string is palindrome or not


Introduction:

Programming is a tool that has enabled us to solve numerous problems and automate several tasks. There are several programming languages, each with its own syntax and features. One such language is QBASIC, which is an ideal language for beginners to learn programming concepts. In this article, we will learn how to write a QBASIC program to input any string and check whether the input string is a palindrome or not.

What is a Palindrome?

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:

  1. Accept a string as input from the user.
  2. Initialize two variables, one at the beginning of the string and the other at the end of the string.
  3. Compare the characters at the beginning and end of the string.
  4. If they are the same, move both the variables one character to the right and left, respectively, and continue the comparison.
  5. 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


In conclusion, QBASIC is an easy-to-learn programming language that is suitable for beginners. We learned how to write a QBASIC program to check whether a string is a palindrome or not. The algorithm to check for palindromes is simple and involves comparing the characters at the beginning and end of the string. By following the steps in the algorithm, we can easily write a QBASIC program to check for palindromes.


You can also Read:


No comments:

Post a Comment

Post Top Ad

Your Ad Spot

Pages