' c_strings.pb — C string functions simulation
#COMPILE EXE
#DIM ALL

FUNCTION PBMAIN() AS LONG
    LOCAL strA AS STRING
    LOCAL strB AS STRING
    LOCAL strC AS STRING
    LOCAL result AS LONG
    LOCAL i AS LONG
    LOCAL j AS LONG
    LOCAL charPos AS LONG
    LOCAL cmpResult AS LONG
    LOCAL lenA AS LONG
    LOCAL lenB AS LONG
    LOCAL foundChar AS LONG
    LOCAL concatLen AS LONG
    LOCAL iter AS LONG
    LOCAL numTests AS LONG
    LOCAL searchChar AS STRING
    LOCAL scanPos AS LONG
    LOCAL srcIdx AS LONG
    LOCAL dstIdx AS LONG

    PRINT "=== C String Functions Simulation ==="

    strA = "Hello"
    strB = "World"
    strC = "Hello"

    PRINT "strlen:"
    lenA = LEN(strA)
    lenB = LEN(strB)
    PRINT "  strlen(" + strA + ") = " + STR$(lenA)
    PRINT "  strlen(" + strB + ") = " + STR$(lenB)

    PRINT "strcmp:"
    IF strA = strC THEN
        cmpResult = 0
    ELSE
        IF strA > strC THEN
            cmpResult = 1
        ELSE
            cmpResult = -1
        END IF
    END IF
    PRINT "  strcmp(A, C) = " + STR$(cmpResult)

    IF strA = strB THEN
        cmpResult = 0
    ELSE
        IF strA > strB THEN
            cmpResult = 1
        ELSE
            cmpResult = -1
        END IF
    END IF
    PRINT "  strcmp(A, B) = " + STR$(cmpResult)

    PRINT "strcat:"
    strA = "Hello"
    strB = "World"
    strA = strA + " "
    strA = strA + strB
    PRINT "  strcat(A, B) = " + strA

    PRINT "strchr:"
    strA = "Hello World"
    searchChar = "o"
    charPos = INSTR(strA, searchChar)
    IF charPos > 0 THEN
        PRINT "  First '" + searchChar + "' in '" + strA + "' at pos " + STR$(charPos)
    ELSE
        PRINT "  '" + searchChar + "' not found"
    END IF

    searchChar = "z"
    charPos = INSTR(strA, searchChar)
    IF charPos > 0 THEN
        PRINT "  First 'z' at pos " + STR$(charPos)
    ELSE
        PRINT "  'z' not found"
    END IF

    PRINT "strstr:"
    strA = "The quick brown fox"
    strB = "quick"
    charPos = INSTR(strA, strB)
    IF charPos > 0 THEN
        PRINT "  '" + strB + "' found in '" + strA + "' at pos " + STR$(charPos)
    ELSE
        PRINT "  '" + strB + "' not found"
    END IF

    PRINT "strcpy:"
    strA = "Source string"
    strB = strA
    PRINT "  strcpy: " + strB
    strC = "New destination"
    strC = strA
    PRINT "  strcpy: " + strC

    PRINT "=== End cstrings ==="
    FUNCTION = 0
END FUNCTION
