' Custom String Functions — reverse, palindrome, word_count, split
#COMPILE EXE
#DIM ALL

FUNCTION PBMAIN() AS LONG
    LOCAL s AS STRING
    LOCAL rev AS STRING
    LOCAL upper AS STRING
    LOCAL lower AS STRING
    LOCAL trimmed AS STRING
    LOCAL part1 AS STRING
    LOCAL part2 AS STRING
    LOCAL part3 AS STRING
    LOCAL i AS LONG
    LOCAL j AS LONG
    LOCAL n AS LONG
    LOCAL current AS STRING
    LOCAL ch AS STRING
    LOCAL acode AS LONG
    LOCAL inWord AS LONG
    LOCAL count AS LONG
    LOCAL isPal AS LONG
    LOCAL startPos AS LONG
    LOCAL endPos AS LONG

    s = "Hello World"
    PRINT "=== STRING LIBRARY ==="

    rev = ""
    FOR i = LEN(s) TO 1 STEP -1
        rev = rev & MID$(s, i, 1)
    NEXT i
    PRINT "Reverse of '" & s & "' = '" & rev & "'"

    upper = ""
    FOR i = 1 TO LEN(s)
        ch = MID$(s, i, 1)
        acode = ASC(ch)
        IF acode >= 97 AND acode <= 122 THEN
            upper = upper & CHR$(acode - 32)
        ELSE
            upper = upper & ch
        END IF
    NEXT i
    PRINT "UPPER: " & upper

    lower = ""
    FOR i = 1 TO LEN(s)
        ch = MID$(s, i, 1)
        acode = ASC(ch)
        IF acode >= 65 AND acode <= 90 THEN
            lower = lower & CHR$(acode + 32)
        ELSE
            lower = lower & ch
        END IF
    NEXT i
    PRINT "lower: " & lower

    n = LEN("racecar")
    isPal = 1
    FOR i = 1 TO n / 2
        IF MID$("racecar", i, 1) <> MID$("racecar", n - i + 1, 1) THEN
            isPal = 0
            EXIT FOR
        END IF
    NEXT i
    IF isPal THEN PRINT "'racecar' is a palindrome" ELSE PRINT "'racecar' is NOT a palindrome"

    n = LEN("hello")
    isPal = 1
    FOR i = 1 TO n / 2
        IF MID$("hello", i, 1) <> MID$("hello", n - i + 1, 1) THEN
            isPal = 0
            EXIT FOR
        END IF
    NEXT i
    IF isPal THEN PRINT "'hello' is a palindrome" ELSE PRINT "'hello' is NOT a palindrome"

    count = 0
    inWord = 0
    FOR i = 1 TO LEN(s)
        ch = MID$(s, i, 1)
        IF ch <> " " THEN
            IF inWord = 0 THEN
                count = count + 1
                inWord = 1
            END IF
        ELSE
            inWord = 0
        END IF
    NEXT i
    PRINT "Word count of '" & s & "' = " & STR$(count)

    trimmed = "  spaced text  "
    startPos = 1
    DO
        IF startPos > LEN(trimmed) THEN EXIT DO
        IF NOT (MID$(trimmed, startPos, 1) = " ") THEN EXIT DO
        startPos = startPos + 1
    LOOP
    endPos = LEN(trimmed)
    DO
        IF endPos < 1 THEN EXIT DO
        IF NOT (MID$(trimmed, endPos, 1) = " ") THEN EXIT DO
        endPos = endPos - 1
    LOOP
    IF startPos <= endPos THEN
        trimmed = MID$(trimmed, startPos, endPos - startPos + 1)
    ELSE
        trimmed = ""
    END IF
    PRINT "Trimmed: '" & trimmed & "'"

    count = 0
    FOR i = 1 TO LEN(s)
        ch = MID$(s, i, 1)
        IF ch = "l" THEN count = count + 1
    NEXT i
    PRINT "CharCount of 'l' in '" & s & "' = " & STR$(count)

    current = ""
    part1 = ""
    part2 = ""
    part3 = ""
    n = LEN("apple,banana,cherry")
    j = 1
    i = 0
    DO
        IF j > n THEN EXIT DO
        ch = MID$("apple, banana, cherry")
        IF ch = "," THEN
            i = i + 1
            IF i = 1 THEN part1 = current
            IF i = 2 THEN part2 = current
            current = ""
        ELSE
            current = current & ch
        END IF
        j = j + 1
    LOOP
    IF LEN(current) > 0 THEN
        i = i + 1
        IF i = 1 THEN part1 = current
        IF i = 2 THEN part2 = current
        IF i = 3 THEN part3 = current
    END IF
    PRINT "Split into " & STR$(i) & " parts:"
    PRINT "  [1] " & part1
    PRINT "  [2] " & part2
    PRINT "  [3] " & part3
    PRINT

    PRINT "String library complete."
    FUNCTION = 0
END FUNCTION
