Monday, January 25, 2016

Check if a String is Palindrome

The best way to learn a new computer language is by solving puzzles, games & code challenges. It's so much fun apart from the satisfaction you get once you crack them! Solving puzzles can push your language skills to the next level apart from improving your problem solving skills. From my experience I can tell you that in the beginning they're in fact a tough-nut-to-crack but once you persist enough to solve a few of them... it's kinda addictive! Start with the simple ones... and don't quit! 

For impatient learners who don't like preaching:

Play with the code and see the results in your browser - Once this page is open, click run and follow the instructions.

Another post that might interest you:


Tip - Take loads of break while solving puzzles. Sometimes you might need to take a break for a whole day so that you can reset your thoughts into a different direction. Also, if you've solved a puzzle in a single sitting, it's not a puzzle in the first place :) 

In this post we'll see how to find if an input string is a palindrome or not using Golang.

What is a Palindrome?

A palindrome is a word, phrase, number, or other sequence of characters which reads the same backward or forward.

Examples of Palindrome
      • Madam
      • Civic
      • Noon
      • Refer
      • Rotor
      • Level
      • Malayalam
      • 1230321
Problem# 1

Enter a string and find out if the string is a Palindrome or not.

Solution# 1


1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package main

import (
 "fmt"
 "strings"
)

func main() {

 var ip string
 fmt.Println("Enter string:")
 fmt.Scanf("%s\n", &ip)
 ip = strings.ToLower(ip)
 fmt.Println(isP(ip))
}
//Function to test if the string entered is a Palindrome
func isP(s string) string { mid := len(s) / 2 last := len(s) - 1 for i := 0; i < mid; i++ { if s[i] != s[last-i] { return "NO. It's not a Palimdrome." } } return "YES! You've entered a Palindrome" }


Now let us build an advanced version of the problem where we can see a practical usage of Slices.

Problem# 2

Enter fixed number of strings, each in a new line, and find out if they are palindrome or not.


Solution# 2

To understand this solution you must have working knowledge of Slices in Go Programming.


package main

import (
 "fmt"
 "strings"
)

func main() {
 var n int
 var ip string
 fmt.Println("How many strings you want to test?")
 fmt.Scanf("%d\n", &n)
 strSlice := make([]string, 0)
 for i := 0; i < n; i++ {

  fmt.Printf("Enter %d string\n", i+1)
  fmt.Scanf("%s\n", &ip)
  ip := strings.ToLower(ip)
  strSlice = append(strSlice, ip)
 }
 for _, v := range strSlice {
  fmt.Println(isP(v))
 }
}

//Function to test if the string entered is a Palindrome
func isP(s string) string {

 mid := len(s) / 2
 last := len(s) - 1
 for i := 0; i < mid; i++ {
  if s[i] != s[last-i] {
   return "NO. " + s + " is Not a Palindrome"
  }
 }
 return "YES. " + s + " is a Palindrome"
}

Output# 2




Play with the above code

Exercise

If you find this interesting you can improve this code:

  1. To take care of the null/empty input strings.
  2. To return appropriate message if number of strings to test is entered zero.

No comments:

Post a Comment