Search code examples
cunicodeencodingiconv

Transliterate accented letters in C


Is it possible to transliterate accents only using iconv?

iconv_t iconv = iconv_open("ASCII//TRANSLIT", "utf-8");

For example I want to transliterate (Spanish) á, é, í, ó to a, e, i, o, but I need to keep ñ. Maybe is there a way to set skipped letters in iconv?

I know that I can use simple replacement, but first I want to know if there are any better solutions.


Solution

  • You cannot achieve the behaviour you've described in your question by simply utilizing the iconv tool since it doesn't provide an option for skipping a specific character. What you could do is create a lookup table (considering your input will be only in UTF8 Latin1).

    The following piece of code could do the trick:

     char* stripAccents(char* text) {
        char *p = text;
        unsigned char c = 0; 
        const char* trans = "AAAAAAECEEEEIIIIDÑOOOOOx0UUUUYPsaaaaaaeceeeeiiiiOñooooo/0uuuuypy";
        while ( *(p) != '\0' ) 
        {
            c = *(p);
            if ( c >= 192 ) {
                *(p) = trans [ c - 192 ];
            }
            ++p; 
        }
        return text;
      }