Write a qbasic program that reverse word enter by user i.e Nepal is lapeN
REM Program to reverse a word
CLS
INPUT " Enter any word "; W$
FOR I = LEN(W$) TO STEP -1
E$ I= LEN(W$, I, 1)
Reverse$=Reverse$ + E$
NEXT I
PRINT " The reverse of "; W$ ; " is " ; Reverse$
END
Output
Enter any word? NEPAL
The reverse of NEPAL is LAPEN
Expalation
CLS Statement clear the screen or erase the output of previous program. INPUT Statement accept the word from the user and store in W$ string variable. i,e if user enter 'NEPAL' at time the execution of program the W$= "NEPAL". MID$ Function help to extract one letter from the word 'NEPAL' OF I position of for loop (i.e. from last letter L) and store in E$ string variable. Reverse$ =Reverse$ + E$ Statement concates the extract letter one by one. When loop terminate the words is reverse of it (i.e NEPAL is LAPEN) .
How this program works ?
Accept word from user. by INPUT statement
Enter any word? NEPAL i.e W$= NEPAL
Now for loop start with starting value I= len(W$) is 5
Loop 1 I=5
E$= MID$(W$,I,1) i.e E$= MID$(W$,5,1) i.e E$='L'
Reverse$=Reverse$+E$ i.e Reverse$ = ' ' + 'L' i.e Reverse$= 'L'
next I i.e Loop 2 with I= 4
E$= MID$(W$,I,1) i.e E$= MID$(W$,4,1) i.e E$='A'
Reverse$=Reverse$+E$ i.e Reverse$ = 'L' + 'A' i.e Reverse$= 'LA'
next I i.e Loop 3 with I= 3
E$= MID$(W$,I,1) i.e E$= MID$(W$,3,1) i.e E$='P'
Reverse$=Reverse$+E$ i.e Reverse$ = 'LA' + 'P' i.e Reverse$= 'LAP'
next I i.e Loop 4 with I= 2
E$= MID$(W$,I,1) i.e E$= MID$(W$,2,1) i.e E$='E'
Reverse$=Reverse$+E$ i.e Reverse$ = 'LAP' + 'E' i.e Reverse$= 'LAPE'
next I i.e Loop 5 with I= 1
E$= MID$(W$,I,1) i.e E$= MID$(W$,1,1) i.e E$='N'
Reverse$=Reverse$+E$ i.e Reverse$ = 'LAPE' + 'N' i.e Reverse$= 'LAPEN'
next I i.e Loop 6 with I= 0 Values of I not satisfy so loop terminate
now next statement is
PRINT " The reverse of "; W$ ; " is " ; Reverse$ i.e The reverse of NEPAL is LAPEN
Last statement END
Terminate Program