' String operations demo (WSTRING removed for PBXA64)
#COMPILE EXE
#DIM ALL

SUB StringBasics()
    LOCAL s AS STRING
    LOCAL i AS LONG
    s = "Hello, World!"
    PRINT "=== STRING BASICS ==="
    PRINT "s = "; s
    PRINT "LEN(s) = "; LEN(s)
    PRINT "First char: "; ASC(s)
    PRINT
    FOR i = 1 TO LEN(s)
        PRINT MID$(s, i, 1); "("; ASC(MID$(s, i, 1)); ") ";
        IF i MOD 8 = 0 THEN PRINT
    NEXT i
    PRINT
    PRINT
END SUB

SUB WstringBasics()
    LOCAL ws AS STRING
    ws = "Wide String Test (using STRING)"
    PRINT "ws = "; ws
    PRINT "LEN(ws) = "; LEN(ws)
    PRINT
END SUB

SUB StringManipulation()
    LOCAL s AS STRING
    LOCAL rev AS STRING
    LOCAL upper AS STRING
    LOCAL lower AS STRING
    LOCAL i AS LONG
    LOCAL ch AS STRING
    LOCAL acode AS LONG
    s = "PowerBASIC x64 Compiler"
    PRINT "=== STRING MANIPULATION ==="
    PRINT "Original: "; s
    rev = ""
    FOR i = LEN(s) TO 1 STEP -1
        rev = rev + MID$(s, i, 1)
    NEXT i
    PRINT "Reversed: "; 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
    PRINT
END SUB

SUB StringCompare()
    LOCAL a AS STRING
    LOCAL b AS STRING
    LOCAL c AS STRING
    a = "apple"
    b = "apple"
    c = "banana"
    PRINT "=== STRING COMPARISON ==="
    PRINT "a = "; a; ", b = "; b; ", c = "; c
    IF a = b THEN PRINT "a = b  (equal)"
    IF a <> c THEN PRINT "a <> c (not equal)"
    IF a < c THEN PRINT "a < c  (apple < banana)"
    IF c > a THEN PRINT "c > a  (banana > apple)"
    PRINT
END SUB

SUB PaddingAndFormatting()
    LOCAL i AS LONG
    LOCAL s AS STRING
    PRINT "=== PADDING & FORMATTING ==="
    PRINT "Left padding with spaces:"
    FOR i = 1 TO 10
        PRINT SPACE$(10 - i); "*"
    NEXT i
    PRINT
    PRINT "Right padding with dots:"
    FOR i = 1 TO 10
        s = STR$(i) + ". "
        PRINT s; SPACE$(40 - LEN(s)); "OK"
    NEXT i
    PRINT
    PRINT "FORMAT$ examples:"
    PRINT "12345 formatted: "; STR$(12345)
    PRINT "3.14159 formatted: "; STR$(3.14159)
    PRINT "Hex 255: "; STR$(255)
    PRINT
END SUB

SUB WordWrapping()
    LOCAL text AS STRING
    LOCAL outLine AS STRING
    LOCAL i AS LONG
    LOCAL ch AS STRING
    LOCAL lineLen AS LONG
    text = "This is a demonstration of word wrapping in a console application. The text will be broken at word boundaries to fit within a specified column width."
    lineLen = 40
    PRINT "=== WORD WRAPPING (width="; lineLen; ") ==="
    outLine = ""
    FOR i = 1 TO LEN(text)
        ch = MID$(text, i, 1)
        IF ch = " " AND LEN(outLine) + 1 > lineLen THEN
            PRINT outLine
            outLine = ""
        ELSE
            outLine = outLine + ch
            IF LEN(outLine) >= lineLen THEN
                PRINT outLine
                outLine = ""
            END IF
        END IF
    NEXT i
    IF LEN(outLine) > 0 THEN PRINT outLine
    PRINT
END SUB

SUB CharEncodingDemo()
    LOCAL i AS LONG
    PRINT "=== ASCII/ANSI CHARACTER TABLE (32-126) ==="
    PRINT "Dec  Chr   Dec  Chr   Dec  Chr   Dec  Chr"
    FOR i = 32 TO 95
        PRINT STR$(i); "  "; CHR$(i); "    "; STR$(i + 32); "  "; CHR$(i + 32); "    "
        IF (i - 32) MOD 4 = 3 THEN PRINT
    NEXT i
    PRINT
    PRINT
END SUB

FUNCTION PBMAIN() AS LONG
    PRINT "================================"
    PRINT "String Operations Demo"
    PRINT "================================"
    PRINT
    StringBasics()
    WstringBasics()
    StringManipulation()
    StringCompare()
    PaddingAndFormatting()
    WordWrapping()
    CharEncodingDemo()
    PRINT "String operations complete."
    FUNCTION = 0
END FUNCTION