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:
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).
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).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"unicode" | |
) | |
func main() { | |
var ip string | |
fmt.Println("Enter any word") | |
fmt.Scanf("%s\n", &ip) | |
fmt.Println(IsPalindrome(ip)) | |
} | |
func IsPalindrome(s string) string { | |
var letters []rune | |
//var str string | |
for _, r := range s { | |
if unicode.IsLetter(r) { | |
letters = append(letters, unicode.ToLower(r)) | |
} | |
} | |
for i := range letters { | |
if letters[i] != letters[len(letters)-1-i] { | |
return "No! It's not a palindrome" | |
} | |
} | |
return "Yes! It's a Palindrome" | |
} |