Saturday, October 22, 2016

Qbasic Program: Palindrome String



159. WAP to input any string and reverse it.
CLS
INPUT "ENTER ANY STRING"; S$

FOR I = LEN(S$) TO 1 STEP -1
B$ = MID$(S$, I, 1)
W$ = W$ + B$
NEXT I
PRINT "REVERSED STRING IS "; W$
END

USING SUB PROCEDURE

DECLARE SUB REV (S$)
CLS
INPUT "ENTER ANY STRING"; S$
CALL REV(S$)
END
SUB REV (S$)
FOR I = LEN(S$) TO 1 STEP -1
B$ = MID$(S$, I, 1)
W$ = W$ + B$
NEXT I
PRINT "REVERSED STRING IS "; W$
END SUB

USING FUNCTION PROCEDURE

DECLARE FUNCTION REV$ (S$)
CLS
INPUT "ENTER ANY STRING"; S$
PRINT "REVERSED STRING IS "; REV$(S$)
END

FUNCTION REV$ (S$)
FOR I = LEN(S$) TO 1 STEP -1
B$ = MID$(S$, I, 1)
W$ = W$ + B$
NEXT I
REV$ = W$
END FUNCTION
160.  WAP to input any string and check whether the given string is palindrome or not.

CLS
INPUT "ENTER ANY STRING"; S$
FOR I = LEN(S$) TO 1 STEP -1
B$ = MID$(S$, I, 1)
W$ = W$ + B$
NEXT I
IF S$ = W$ THEN
PRINT  S$; “IS PALINDROME”
ELSE
PRINT S$; “IS NOT PALINDROME”
END IF
END

USING SUB PROCEDURE

DECLARE SUB REV (S$)
CLS
INPUT "ENTER ANY STRING"; S$
CALL REV(S$)
END

SUB REV (S$)
FOR I = LEN(S$) TO 1 STEP -1
B$ = MID$(S$, I, 1)
W$ = W$ + B$
NEXT I
IF S$ = W$ THEN
PRINT  S$; “IS PALINDROME”
ELSE
PRINT S$; “IS NOT PALINDROME”
END IF
END SUB

USING FUNCTION PROCEDURE

DECLARE FUNCTION REV$ (S$)
CLS
INPUT "ENTER ANY STRING"; S$
C$ = REV$(S$)
IF S$ = C$ THEN
PRINT  S$; “IS PALINDROME”
ELSE
PRINT S$; “IS NOT PALINDROME”
END IF
END

FUNCTION REV$ (S$)
FOR I = LEN(S$) TO 1 STEP -1
B$ = MID$(S$, I, 1)
W$ = W$ + B$
NEXT I
REV$ = W$
END FUNCTION

3 comments: