#COMPILE EXE
#DIM ALL

FUNCTION PBMAIN() AS LONG
    LOCAL first_word AS STRING
    LOCAL last_word AS STRING
    LOCAL first_code AS STRING
    LOCAL last_code AS STRING
    LOCAL i AS LONG
    LOCAL c AS LONG
    LOCAL prev_char AS LONG
    LOCAL temp_str AS STRING

    PRINT "NYSIIS Phonetic Encoding"
    PRINT "========================"
    PRINT ""

    first_word = "MacDonald"
    first_code = ""
    i = 1

    WHILE i <= LEN(first_word)
        c = ASC(MID$(UCASE$(first_word), i, 1))
        IF i = 1 THEN
            IF c = 77 THEN
                first_code = first_code + "M"
            ELSEIF c = 75 THEN
                IF i + 1 <= LEN(first_word) AND ASC(MID$(UCASE$(first_word), i + 1, 1)) = 78 THEN
                    first_code = first_code + "N"
                    i = i + 1
                ELSE
                    first_code = first_code + "C"
                END IF
            ELSEIF c = 80 THEN
                IF i + 1 <= LEN(first_word) AND ASC(MID$(UCASE$(first_word), i + 1, 1)) = 72 THEN
                    first_code = first_code + "F"
                    i = i + 1
                ELSE
                    first_code = first_code + "P"
                END IF
            ELSEIF c = 83 THEN
                first_code = first_code + "S"
            ELSE
                first_code = first_code + CHR$(c)
            END IF
        ELSE
            prev_char = ASC(MID$(UCASE$(first_word), i - 1, 1))
            IF c = 69 OR c = 73 OR c = 79 OR c = 85 THEN
                c = 65
            END IF
            IF c = 69 AND prev_char = 69 THEN
            ELSEIF c = 73 AND prev_char = 73 THEN
            ELSEIF c = 79 AND prev_char = 79 THEN
            ELSEIF c = 85 AND prev_char = 85 THEN
            ELSEIF c = 65 AND (prev_char = 65 OR prev_char = 69) THEN
            ELSE
                first_code = first_code + CHR$(c)
            END IF
        END IF
        i = i + 1
    WEND

    PRINT "NYSIIS('" + first_word + "') = '" + first_code + "'"

    last_word = "Macdonald"
    last_code = ""
    i = 1
    WHILE i <= LEN(last_word)
        c = ASC(MID$(UCASE$(last_word), i, 1))
        IF i = 1 THEN
            last_code = last_code + CHR$(c)
        ELSE
            IF c = 69 OR c = 73 OR c = 79 OR c = 85 THEN c = 65
            IF c = 72 OR c = 87 THEN
                IF ASC(MID$(UCASE$(last_word), i - 1, 1)) = 65 THEN
                ELSE
                    last_code = last_code + CHR$(c)
                END IF
            ELSE
                last_code = last_code + CHR$(c)
            END IF
        END IF
        i = i + 1
    WEND
    PRINT "NYSIIS('" + last_word + "') = '" + last_code + "'"

    PRINT "Done."
    FUNCTION = 0
END FUNCTION
