' STRING vs WSTRING — conversion, encoding handling
#COMPILE EXE
#DIM ALL

SUB StringBasics()
    LOCAL s AS STRING
    LOCAL ws AS WSTRING
    LOCAL i AS LONG
    s = "Hello, World!"
    PRINT "=== STRING BASICS ==="
    PRINT "s = " & s
    PRINT "LEN(s) = " & STR$(LEN(s))
    PRINT "SIZEOF(s) = " & STR$(SIZEOF(s))
    PRINT "ASC(s, 1) = " & STR$(ASC(s, 1))
    PRINT "ASC(s, 7) = " & STR$(ASC(s, 7))
    PRINT
    FOR i = 1 TO LEN(s)
        PRINT MID$(s, i, 1) & "(" & STR$(ASC(s, i)) & ") "
        IF i MOD 8 = 0 THEN PRINT
    NEXT i
    PRINT
    PRINT
END SUB

SUB WstringBasics()
    LOCAL ws AS WSTRING
    ws = "Wide String Test"
    PRINT "=== WSTRING BASICS ==="
    PRINT "ws = " & ws
    PRINT "LEN(ws) = " & STR$(LEN(ws))
    PRINT "SIZEOF(ws) = " & STR$(SIZEOF(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 s AS STRING
    LOCAL i AS LONG
    PRINT "=== PADDING & FORMATTING ==="
    PRINT "Left padding with spaces:"
    FOR i = 1 TO 10
        PRINT STRING$(10 - i, 32) & REPEAT$("*", i)
    NEXT i
    PRINT
    PRINT "Right padding with dots:"
    FOR i = 1 TO 10
        s = STR$(i) & ". " & REPEAT$("Item ", i)
        PRINT s & STRING$(40 - LEN(s), 46) & "OK"
    NEXT i
    PRINT
    PRINT "FORMAT$ examples:"
    PRINT "12345 formatted: " & FORMAT$(12345, "###,###")
    PRINT "3.14159 formatted: " & FORMAT$(3.14159, "##.####")
    PRINT "Hex 255: " & FORMAT$(255, "&H")
    PRINT
END SUB

SUB WordWrapping()
    LOCAL text AS STRING
    LOCAL line 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=" & STR$(lineLen) & ") ==="
    line = ""
    FOR i = 1 TO LEN(text)
        ch = MID$(text, i, 1)
        IF ch = " " AND LEN(line) + 1 > lineLen THEN
            PRINT line
            line = ""
        ELSE
            line = line & ch
            IF LEN(line) >= lineLen THEN
                PRINT line
                line = ""
            END IF
        END IF
    NEXT i
    IF LEN(line) > 0 THEN PRINT line
    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"
    PRINT STRING$(50, 61)
    FOR i = 32 TO 95
        PRINT FORMAT$(i, "###") & "  " & CHR$(i) & "    " & FORMAT$(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 & WSTRING Operations"
    PRINT "================================"
    PRINT
    CALL StringBasics()
    CALL WstringBasics()
    CALL StringManipulation()
    CALL StringCompare()
    CALL PaddingAndFormatting()
    CALL WordWrapping()
    CALL CharEncodingDemo()
    PRINT "String operations complete."
    FUNCTION = 0
END FUNCTION
