Palindrome in Go
A palindrome is a number/string which is similar when read from the front and from the rear.
Problem statement
Given an Number, check whether it is palindrome or not.
Solution
package main import "fmt" func checkPalindrome(num int) bool { input_num := num var remainder int res := 0 for num > 0 { remainder = num % 10 res = (res * 10) + remainder num = num / 10 } if input_num == res { return true } else { return false } } func main() { if checkPalindrome(122) { fmt.Println("Number is palindrome") } else { fmt.Println("Number is not palindrome") } }
$ Number is not palindrome $ |
Problem statement
Given a string, check whether it is palindrome or not.
Solution
package main import "fmt" func isPalindrome(str string) bool { for i := 0; i < len(str); i++ { j := len(str) - 1 - i if str[i] != str[j] { return false } } return true } func main() { if isPalindrome("ana") { fmt.Println("String is palindrome") } else { fmt.Println("String is not palindrome") } }
$ String is palindrome $ |
If you like the post Palindrome in Go, please share your feedback!
also see
C Programming language |
Go Programming language |
Linked List | Array |
Stack | Queue |
Puzzle | Reasoning |
Aptitude | HTML |