Ensure first character of strings is uppercase in QB64

205 Views Asked by At

Is there a way to make sure the first char in FirstName and LastName is uppercase only?

DIM FirstName AS STRING
DIM LastName AS STRING

CLS

INPUT "Enter First Name: ", FirstName
INPUT "Enter Last Name: ", LastName
2

There are 2 best solutions below

2
On

You can use the string functions LEFT$ and UCASE$ in tandem:

first$ = LEFT$(FirstName, 1)
last$ = LEFT$(LastName, 1)
IF first$ <> UCASE$(first$) OR last$ <> UCASE$(last$) THEN
    PRINT "error: first letter of names must be capitalized"
    END
END IF

If you don't want the program to exit, you could just change it to uppercase yourself by using the MID$ statement:

first$ = LEFT$(FirstName, 1)
last$ = LEFT$(LastName, 1)
MID$(FirstName, 1, 1) = UCASE$(first$)
MID$(LastName, 1, 1) = UCASE$(last$)

For more information on string manipulation and conversions and other information, see the QB64 wiki.

1
On

Check this out:

A$ = LEFT$(FirstName$, 1) // get first character
B = ASC(A$) //change it to ascii
IF B >= 65 AND B <= 90 THEN //is it uppercase?
CK$ = "FIRST CHARACTER IS UPPERCASE"
END IF
PRINT CK$