QBASIC Program to print the given string in reverse order

3 years ago

In this example, you will learn to reverse the string entered by the user. This program takes an string input from the user. Then for loop is used until length of string is <> 0. In each iteration of the loop, the r$ string variable reads the string from last character with the help of MID$ string library function ans store it in r$. And b$ string veriable will store the letters of r$ one by one. String stored in b$ will be in reverse order. 

CLS
INPUT "Enter any String "; s$
FOR i = LEN(s$) TO 1 STEP -1
    r$ = MID$(s$, i, 1)
    b$ = b$ + r$
NEXT i
PRINT "Reverse Order = "; b$
END

Using SUB .... END SUB

DECLARE SUB revf(s$)
CLS
INPUT "Enter any String "; s$
CALL rev(s$)
END

SUB rev (s$)
    FOR i = LEN(s$) TO 1 STEP -1
        r$ = MID$(s$, i, 1)
        b$ = b$ + r$
    NEXT i
    PRINT "Reverse Order = "; b$
END SUB

Using FUNCTION .... END FUNCTION

DECLARE FUNCTION rev$(s$)
CLS
INPUT "Enter any string "; s$
re$ = rev$(s$)
PRINT "Reverse Order = "; re$
END

FUNCTION rev$ (s$)
    FOR i = LEN(s$) TO 1 STEP -1
        r$ = MID$(s$, i, 1)
        b$ = b$ + r$
    NEXT i
    rev$ = b$
END FUNCTION
  9177
Please Login to Post a Comment