Task: NYSIIS

C: #include <iostream>   
#include <iomanip>    
#include <string>

std::string NYSIIS( std::string const& str )
{
    std::string s, out;
    s.reserve( str.length() );
    for( auto const c : str )
    {
        if( c >= 'a' && c <= 'z' )
            s += c - ('a' - 'A');
        else if( c >= 'A' && c <= 'Z' )
            s += c;
    }

    auto replace = []( char const * const from, char const* to, char* const dst ) -> bool
    {
        auto const n = strlen( from );
        if( strncmp( from, dst, n ) == 0 )
        {
            strncpy( dst, to, n );
            return true;
        }
        return false;
    };

    auto multiReplace = []( char const* const* from, char const* to, char* const dst ) -> bool
    {
        auto const n = strlen( *from );
        for( ; *from; ++from )
            if( strncmp( *from, dst, n ) == 0 )
            {
                memcpy( dst, to, n );
                return true;
            }
        return false;
    };

    auto isVowel = []( char const c ) -> bool
    {
        return c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U';
    };

    size_t n = s.length();
    replace( "MAC", "MCC", &s[0] );
    replace( "KN", "NN", &s[0] );
    replace( "K", "C", &s[0] );
    char const* const prefix[] = { "PH", "PF", 0 };
    multiReplace( prefix, "FF", &s[0] );
    replace( "SCH", "SSS", &s[0] );

    char const* const suffix1[] = { "EE", "IE", 0 };
    char const* const suffix2[] = { "DT", "RT", "RD", "NT", "ND", 0 };
    if( multiReplace( suffix1, "Y", &s[n - 2] ) || multiReplace( suffix2, "D", &s[n - 2] ))
    {
        s.pop_back();
        --n;
    }

    out += s[0];

    char* vowels[] = { "A", "E", "I", "O", "U", 0 };
    for( unsigned i = 1; i < n; ++i )
    {
        char* const c = &s[i];
        if( !replace( "EV", "AV", c ) )
            multiReplace( vowels, "A", c );
        replace( "Q", "G", c );
        replace( "Z", "S", c );
        replace( "M", "N", c );
        if( !replace( "KN", "NN", c ))
            replace( "K", "C", c );
        replace( "SCH", "SSS", c );
        replace( "PH", "FF", c );
        if( *c == 'H' && (!isVowel( s[i - 1] ) || i + 1 >= n || !isVowel( s[i + 1] )))
            *c = s[i - 1];
        if( *c == 'W' && isVowel( s[i - 1] ))
            *c = 'A';
        if( out.back() != *c )
            out += *c;
    }

    if( out.back() == 'S' || out.back() == 'A' )
        out.pop_back();
    n = out.length() - 2;
    if( out[n] == 'A' && out[n + 1] == 'Y' )
        out = out.substr( 0, n ) + "Y";

    return out;
}

int main()
{
    static char const * const names[][2] = {
    { "Bishop", "BASAP" },
    { "Carlson", "CARLSAN" },
    { "Carr", "CAR" },
    { "Chapman", "CAPNAN" },
    { "Franklin", "FRANCLAN" },
    { "Greene", "GRAN" },
    { "Harper", "HARPAR" },
    { "Jacobs", "JACAB" },
    { "Larson", "LARSAN" },
    { "Lawrence", "LARANC" },
    { "Lawson", "LASAN" },
    { "Louis, XVI", "LASXV" },
    { "Lynch", "LYNC" },
    { "Mackenzie", "MCANSY" },
    { "Matthews", "MATA" },
    { "McCormack", "MCARNAC" },
    { "McDaniel", "MCDANAL" },
    { "McDonald", "MCDANALD" },
    { "Mclaughlin", "MCLAGLAN" },
    { "Morrison", "MARASAN" },
    { "O'Banion", "OBANAN" },
    { "O'Brien", "OBRAN" },
    { "Richards", "RACARD" },
    { "Silva", "SALV" },
    { "Watkins", "WATCAN" },
    { "Wheeler", "WALAR" },
    { "Willis", "WALA" },
    { "brown, sr", "BRANSR" },
    { "browne, III", "BRAN" },
    { "browne, IV", "BRANAV" },
    { "knight", "NAGT" },
    { "mitchell", "MATCAL" },
    { "o'daniel", "ODANAL" } };

    for( auto const& name : names )
    {
        auto const code = NYSIIS( name[0] );
        std::cout << std::left << std::setw( 16 ) << name[0] << std::setw( 8 ) << code;
        if( code == std::string( name[1] ))
            std::cout << " ok";
        else
            std::cout << " ERROR: " << name[1] << " expected";
        std::cout << std::endl;
    }

    return 0;
}




Go: package main

import (
    "fmt"
    "strings"
)

type pair struct{ first, second string }

var (
    fStrs = []pair{{"MAC", "MCC"}, {"KN", "N"}, {"K", "C"}, {"PH", "FF"},
        {"PF", "FF"}, {"SCH", "SSS"}}
    lStrs = []pair{{"EE", "Y"}, {"IE", "Y"}, {"DT", "D"}, {"RT", "D"},
        {"RD", "D"}, {"NT", "D"}, {"ND", "D"}}
    mStrs = []pair{{"EV", "AF"}, {"KN", "N"}, {"SCH", "SSS"}, {"PH", "FF"}}
    eStrs = []string{"JR", "JNR", "SR", "SNR"}
)

func isVowel(b byte) bool {
    return strings.ContainsRune("AEIOU", rune(b))
}

func isRoman(s string) bool {
    if s == "" {
        return false
    }
    for _, r := range s {
        if !strings.ContainsRune("IVX", r) {
            return false
        }
    }
    return true
}

func nysiis(word string) string {
    if word == "" {
        return ""
    }
    w := strings.ToUpper(word)
    ww := strings.FieldsFunc(w, func(r rune) bool {
        return r == ' ' || r == ','
    })
    if len(ww) > 1 {
        last := ww[len(ww)-1]
        if isRoman(last) {
            w = w[:len(w)-len(last)]
        }
    }
    for _, c := range " ,'-" {
        w = strings.Replace(w, string(c), "", -1)
    }
    for _, eStr := range eStrs {
        if strings.HasSuffix(w, eStr) {
            w = w[:len(w)-len(eStr)]
        }
    }
    for _, fStr := range fStrs {
        if strings.HasPrefix(w, fStr.first) {
            w = strings.Replace(w, fStr.first, fStr.second, 1)
        }
    }
    for _, lStr := range lStrs {
        if strings.HasSuffix(w, lStr.first) {
            w = w[:len(w)-2] + lStr.second
        }
    }
    initial := w[0]
    var key strings.Builder
    key.WriteByte(initial)
    w = w[1:]
    for _, mStr := range mStrs {
        w = strings.Replace(w, mStr.first, mStr.second, -1)
    }
    sb := []byte{initial}
    sb = append(sb, w...)
    le := len(sb)
    for i := 1; i < le; i++ {
        switch sb[i] {
        case 'E', 'I', 'O', 'U':
            sb[i] = 'A'
        case 'Q':
            sb[i] = 'G'
        case 'Z':
            sb[i] = 'S'
        case 'M':
            sb[i] = 'N'
        case 'K':
            sb[i] = 'C'
        case 'H':
            if !isVowel(sb[i-1]) || (i < le-1 && !isVowel(sb[i+1])) {
                sb[i] = sb[i-1]
            }
        case 'W':
            if isVowel(sb[i-1]) {
                sb[i] = 'A'
            }
        }
    }
    if sb[le-1] == 'S' {
        sb = sb[:le-1]
        le--
    }
    if le > 1 && string(sb[le-2:]) == "AY" {
        sb = sb[:le-2]
        sb = append(sb, 'Y')
        le--
    }
    if le > 0 && sb[le-1] == 'A' {
        sb = sb[:le-1]
        le--
    }
    prev := initial
    for j := 1; j < le; j++ {
        c := sb[j]
        if prev != c {
            key.WriteByte(c)
            prev = c
        }
    }
    return key.String()
}

func main() {
    names := []string{
        "Bishop", "Carlson", "Carr", "Chapman",
        "Franklin", "Greene", "Harper", "Jacobs", "Larson", "Lawrence",
        "Lawson", "Louis, XVI", "Lynch", "Mackenzie", "Matthews", "May jnr",
        "McCormack", "McDaniel", "McDonald", "Mclaughlin", "Morrison",
        "O'Banion", "O'Brien", "Richards", "Silva", "Watkins", "Xi",
        "Wheeler", "Willis", "brown, sr", "browne, III", "browne, IV",
        "knight", "mitchell", "o'daniel", "bevan", "evans", "D'Souza",
        "Hoyle-Johnson", "Vaughan Williams", "de Sousa", "de la Mare II",
    }
    for _, name := range names {
        name2 := nysiis(name)
        if len(name2) > 6 {
            name2 = fmt.Sprintf("%s(%s)", name2[:6], name2[6:])
        }
        fmt.Printf("%-16s : %s\n", name, name2)
    }
}