Programming Fundamentals/Strings/BASIC

strings.bas

edit
' This program splits a given comma-separated name into first and last name
' components and then displays the name.
'
' References:
'     https://www.mathsisfun.com/temperature-conversion.html
'     https://en.wikibooks.org/wiki/BASIC_Programming

DECLARE SUB Main()
DECLARE FUNCTION GetName() AS STRING
DECLARE FUNCTION GetLast(FullName AS STRING) AS STRING
DECLARE FUNCTION GetFirst(FullName AS STRING) AS STRING
DECLARE SUB DisplayName(First AS STRING, Last AS STRING)

Main()

SUB Main()
    DIM FullName AS STRING
    DIM Last AS STRING
    DIM First AS STRING

    FullName = GetName()
    Last = GetLast(FullName)
    First = GetFirst(FullName)
    DisplayName First, Last
END SUB

FUNCTION GetName() AS STRING
    DIM FullName AS STRING
    DIM Index AS INTEGER
    
    DO
        PRINT "Enter name (last, first):"
        LINE INPUT FullName
        Index = INSTR(FullName, ",")
    LOOP WHILE Index < 1
    
    GetName = FullName
END FUNCTION

FUNCTION GetLast(FullName AS STRING) AS STRING
    DIM Index AS INTEGER

    Index = INSTR(FullName, ",")
    IF Index < 1 THEN
        GetLast = ""
    ELSE
        GetLast = MID$(FullName, 1, Index - 1)
    END IF
END FUNCTION

FUNCTION GetFirst(FullName AS STRING) AS STRING
    DIM Index AS INTEGER

    Index = INSTR(FullName, ",")
    IF Index < 1 THEN
        GetFirst = ""
    ELSE
        GetFirst = LTRIM$(MID$(Fullname, Index + 1))
    END IF
END FUNCTION

SUB DisplayName(First AS STRING, Last AS STRING)
    PRINT "Hello " + First + " " + Last + "!"
END SUB

Try It

edit

Copy and paste the code above into one of the following free online development environments or use your own BASIC compiler / interpreter / IDE.

See Also

edit