Sunday, April 23, 2017

Palindrome - Hindi, Chinese, French, Spanish

Recently I realized that the code I shared for Palindrome here has a few limitations. It supports only ASCII character sets (i.e. mainly English alphabet & numbers). It won't support other language palindromes.

See the following palindrome examples from some other major languages:

        • été (French)
          •  दामाद (Hindi)
            • सरस (Hindi)
              • 不得不  (Chinese)
                • abañaba (Spanish)
                  • анилина (Russian)
                    • カジカ (Japanese)

If you input any of the above in our previous code you won't get a correct output. The characters in above examples such as é द   etc. are non ASCII and hence our previous code doesn't work.

Check the above palindromes with this new code in action.

Let us see how to overcome this limitation.

rune: Type rune is basically int32. It generally indicates a Unicode code point.

See the following revised code that works perfectly fine even with non-ASCII characters.
P.S. Reference: The code is adapted from The Go Programming Language (Donovan & Kernighan).