Saturday, October 22, 2016

Qbasic Program: Longest and Shortest String



177.  WAP to enter any ten strings and display the longest strings.
CLS
INPUT "ENTER FIRST STRING"; A$
FOR I = 2 TO 10
INPUT "ENTER NEXT STRING"; B$
IF LEN(B$) > LEN(A$) THEN A$ = B$
NEXT I
PRINT "LONGEST STRING="; A$
END

USING SUB PROCEDURE
DECLARE SUB LON( A$ )
CLS
INPUT "ENTER FIRST STRING"; A$
CALL LON (A$)
END
SUB LON (A$)
FOR I = 2 TO 10
INPUT "ENTER NEXT STRING"; B$
IF LEN(B$) > LEN(A$) THEN A$ = B$
NEXT I
PRINT "LONGEST STRING="; A$
END SUB

USING FUNCTION PROCEDURE

DECLARE FUNCTION LON$( A$ )
CLS
INPUT "ENTER FIRST STRING"; A$
PRINT "LONGEST STRING=";  LON (A$)
END
FUNCTION LON (A$)

FOR I = 2 TO 10
INPUT "ENTER NEXT STRING"; B$
IF LEN(B$) > LEN(A$) THEN A$ = B$
NEXT I
LON$ = A$
END FUNCTION

178.  WAP to enter any ten strings and display the shortest string.
CLS
INPUT "ENTER FIRST STRING"; A$
FOR I = 2 TO 10
INPUT "ENTER NEXT STRING"; B$
IF LEN(B$) < LEN(A$) THEN A$ = B$
NEXT I
PRINT "SHORTEST STRING="; A$
END
USING SUB PROCEDURE
DECLARE SUB SHRT( A$ )
CLS
INPUT "ENTER FIRST STRING"; A$
CALL SHRT (A$)
END
SUB SHRT (A$)
FOR I = 2 TO 10
INPUT "ENTER NEXT STRING"; B$
IF LEN(B$) < LEN(A$) THEN A$ = B$
NEXT I
PRINT "SHORTEST STRING="; A$
END SUB

USING FUNCTION PROCEDURE

DECLARE FUNCTION SHRT$( A$ )
CLS
INPUT "ENTER FIRST STRING"; A$
PRINT "SHORTEST STRING=";  SHRT$(A$)
END
FUNCTION SHRT$(A$)
FOR I = 2 TO 10
INPUT "ENTER NEXT STRING"; B$
IF LEN(B$) < LEN(A$) THEN A$ = B$
NEXT I
SHRT$ = A$
END FUNCTION



179. WAP to enter any ten strings and sort in ascending order.
CLS
DIM N(10) AS STRING
FOR I = 1 TO 10
INPUT "ENTER THE STRINGS"; N(I)
NEXT I
FOR I = 1 TO 10
FOR J = 1 TO 10 - I
IF N(J) > N(J + 1) THEN SWAP N(J), N(J + 1)
NEXT J
NEXT I
PRINT "STRINGS ARRANGED IN ASCENDING ORDER"
FOR I = 1 TO 10
PRINT N(I)
NEXT I
END

USING SUB PROCEDURE
DECLARE SUB SORT (N())
CLS
DIM N(10) AS STRING
FOR I = 1 TO 10
INPUT "ENTER THE STRINGS"; N(I)
NEXT I
CALL SORT(N())
PRINT "STRINGS ARRANGED IN ASCENDING ORDER"
FOR I = 1 TO 10
PRINT N(I)
NEXT I
END
SUB SORT (N())
FOR I = 1 TO 10
FOR J = 1 TO 10 - I
IF N(J) > N(J + 1) THEN SWAP N(J), N(J + 1)
NEXT J
NEXT I

END SUB
180. WAP to enter any ten strings and sort in descending order.
CLS
DIM N(10) AS STRING
FOR I = 1 TO 10
INPUT "ENTER THE STRINGS"; N(I)
NEXT I
FOR I = 1 TO 10
FOR J = 1 TO 10 - I
IF N(J) < N(J + 1) THEN SWAP N(J), N(J + 1)
NEXT J
NEXT I
PRINT "STRINGS ARRANGED IN DESCENDING ORDER"
FOR I = 1 TO 10
PRINT N(I)
NEXT I
END

USING SUB PROCEDURE
DECLARE SUB SORT (N())
CLS
DIM N(10) AS STRING
FOR I = 1 TO 10
INPUT "ENTER THE STRINGS"; N(I)
NEXT I
CALL SORT(N())
PRINT "STRINGS ARRANGED IN DESCENDING ORDER"
FOR I = 1 TO 10
PRINT N(I)
NEXT I
END
SUB SORT (N())
FOR I = 1 TO 10
FOR J = 1 TO 10 - I
IF N(J) < N(J + 1) THEN SWAP N(J), N(J + 1)
NEXT J
NEXT I

END SUB
181. WAP to enter any three strings and print the shortest one.

CLS
INPUT "ENTER FIRST STRING"; A$
INPUT "ENTER SECOND STRING"; B$
INPUT "ENTER THIRD STRING"; C$
IF LEN(A$) < LEN(B$) AND LEN(A$) < LEN(C$) THEN
S$ = A$
IF LEN(B$) < LEN(A$) AND LEN(B$) < LEN(C$) THEN
S$ = B$
ELSE
S$ = C$
END IF
PRINT "SHORTEST STRING="; S$
END

USING SUB PROCEDURE
DECLARE SUB SHRT( A$, B$, C$)
CLS
INPUT "ENTER FIRST STRING"; A$
INPUT "ENTER SECOND STRING"; B$
INPUT "ENTER THIRD STRING"; C$
CALL SHRT$(A$, B$, C$)
END

SUB SHRT$(A$, B$, C$)
IF LEN(A$) < LEN(B$) AND LEN(A$) < LEN(C$) THEN
S$ = A$
IF LEN(B$) < LEN(A$) AND LEN(B$) < LEN(C$) THEN
S$ = B$
ELSE
S$ = C$
END IF
PRINT “SHORTEST STRING=”; S$
END SUB
USING FUNCTION PROCEDURE
DECLARE FUNCTION SHRT$( A$, B$, C$)
CLS
INPUT "ENTER FIRST STRING"; A$
INPUT "ENTER SECOND STRING"; B$
INPUT "ENTER THIRD STRING"; C$
PRINT "SHORTEST STRING="; SHRT$( A$, B$, C$)
END

FUNCTION SHRT$( A$, B$, C$)
IF LEN(A$) < LEN(B$) AND LEN(A$) < LEN(C$) THEN
S$ = A$
IF LEN(B$) < LEN(A$) AND LEN(B$) < LEN(C$) THEN
S$ = B$
ELSE
S$ = C$
END IF
SHRT$ = S$
END FUNCTION

182. WAP to enter any three strings and print the longest one.
CLS
INPUT "ENTER FIRST STRING"; A$
INPUT "ENTER SECOND STRING"; B$
INPUT "ENTER THIRD STRING"; C$
IF LEN(A$) > LEN(B$) AND LEN(A$) > LEN(C$) THEN
G$ = A$
IF LEN(B$) > LEN(A$) AND LEN(B$) > LEN(C$) THEN
G$ = B$
ELSE
G$ = C$
END IF
PRINT "LONGEST STRING="; G$
END

USING SUB PROCEDURE
DECLARE SUB LON ( A$, B$, C$)
CLS
INPUT "ENTER FIRST STRING"; A$
INPUT "ENTER SECOND STRING"; B$
INPUT "ENTER THIRD STRING"; C$
CALL LON(A$, B$, C$)
END

SUB LON (A$, B$, C$)
IF LEN(A$) > LEN(B$) AND LEN(A$) > LEN(C$) THEN
G$ = A$
IF LEN(B$) > LEN(A$) AND LEN(B$) > LEN(C$) THEN
G$ = B$
ELSE
G$ = C$
END IF
PRINT “LONGEST STRING=”; G$
END SUB
USING FUNCTION PROCEDURE
DECLARE FUNCTION LON$( A$, B$, C$)
CLS
INPUT "ENTER FIRST STRING"; A$
INPUT "ENTER SECOND STRING"; B$
INPUT "ENTER THIRD STRING"; C$
PRINT "LONGEST STRING="; LON$( A$, B$, C$)
END
FUNCTION LON$( A$, B$, C$)
IF LEN(A$) > LEN(B$) AND LEN(A$) > LEN(C$) THEN
G$ = A$
IF LEN(B$) > LEN(A$) AND LEN(B$) > LEN(C$) THEN
G$ = B$
ELSE
G$ = C$
END IF
LON$ = S$
END FUNCTION

2 comments: