Programming Fundamentals/Strings/BASIC

From Wikiversity
Jump to navigation Jump to search

strings.bas

[edit | edit source]
' 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 | edit source]

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

For Free Online BASIC IDE, student can use TutorialsPoint for FreeBasic and replit for QBasic.

Lessons

[edit | edit source]
  • Introduction:Development stage: 100% (as of {{{2}}}) Introduction to BASIC, "Hello, World !" program and variables.
  • Functions: Development stage: 100% (as of {{{2}}}) Work with function with and without parameters


See Also

[edit | edit source]

See Also

[edit | edit source]