Golang Program to Check Palimdrome


In this tutorial, we will learn how to check for palindrome in Go programming language. We will check for palindrome for both numbers and strings in separate examples.

A palindrome is a word, number, phrase, or other sequence of symbols that reads the same backwards as forwards, such as the words civic or radar or numbers such as 22\2\22 or sentence such as “Mr. Owl ate my metal worm”.

Example 1: Golang Program code to Show if a Number is palindrome using a Single Function

Syntax

Syntax of For loop
Initialize
for condition {
}
incrementor

The initialization statement is optional and executes before for loop starts.

The condition statement holds a Boolean expression, which is evaluated at the starting of each iteration of the loop. If the value of the conditional statement is true, then the loop executes.

The incrementor statement is executed after the body of the forloop. After the incrementor statement, the condition statement evaluates again if the value of the conditional statement is false, then the loop ends.

Algorithm

  • Step 1 − Import the package fmt.

  • Step 2 − Start function main ().

  • Step 3 − Declare and initialize the variables.

  • Step 4 − Use for loop to analyze the condition.

  • Step 5 − Print the result using fmt.Printf ().

Example

// GOLANG PROGRAM TO CHECK FOR PALINDROME // fmt package allows us to print anything on the screen. package main import "fmt" // start the function main () func main() { fmt.Println("Golang program to check for palindrome") // declare the variables var number,rem,temporary int var reverse int = 0 // initialize the number variable number = 45454 temporary=number // For Loop used for{ rem = number%10 reverse = reverse*10 + rem number /= 10 if(number==0){ break // Break Statement used to exit from loop } } if(temporary==reverse){ fmt.Printf("Number %d is a Palindrome",temporary) }else{ fmt.Printf("Number %d is not a Palindrome",temporary) } // print the result using fmt.Printf () function }

Output

Golang program to check for palindrome
Number 26262 is a Palindrome

Description of the Code

  • In the above program, we first declare the package main.

  • We have imported the fmt package that includes the files of package fmt.

  • Now start the function main () and this function is the entry point of the executable programs. It does not take any argument nor return anything.

  • Declare the four integer variables. Initialize the variable reverse to value 0 and number variable to the value you want.

  • Using for loop  The condition is given inside an if statement and stop execution is mentioned by a break statement.

  • And last printing the result on the screen using fmt.Printf () which formats according to a format specifier and writes to standard output.

Example 2: Golang Program Code to Check if a String is Numeric or not in two Separate Functions

Syntax

for i, j:= range variable{
   // statement..
}

Where I= first index value and j= last index value

i and j are the variables are known as iteration variables and their values are assigned.

The range expression is evaluated once before the starting of the loop.

Algorithm

  • Step 1 − Import the package fmt.

  • Step 2 − Start the function main ().

  • Step 3 − Declare and initialize the string variable.

  • Step 4 − Calling the function Palindrome ().

  • Step 5 − Create the function Palindrome ().

  • Step 6 − Use for loop to analyze the condition.

  • Step 7 − Print the result using fmt.Printf ().

Example

// GOLANG PROGRAM TO CHECK FOR PALINDROME // fmt package allows us to print anything on the screen. package main import "fmt" // start the function main () func main() { // declare and initialize the string str := "MADAM" fmt.Println("Golang program to check palindrome,\n Given Word =",str) // calling the function Palindrome(str) // print the result using fmt.Printf () function fmt.Printf("'%s' is palindrome\n", str) } func Palindrome(str string) bool { lastIdx := len(str) - 1 // using for loop for i := 0; i < lastIdx/2 && i < (lastIdx-i); i++ { if str[i] != str[lastIdx-i] { return false } } return true }

Output

Golang program to check palindrome,
Given Word = MADAM
'MADAM' is palindrome

Description of the Code

  • In the above program, we first declare the package main.

  • We have imported the fmt package that includes the files of package fmt.

  • Now start the function main () and this function is the entry point of the executable programs. It does not take any argument nor return anything.

  • Next, we declare and initialize the string variable to find if it is palindrome.

  • Then we call the function Palindrome() which we created later in the function to check if the given string is palindrome or not.

  • In the above function we use for loop to analyze the condition of the code, where i= first index and variable lastIdx will be the last index. We start the for loop as expected with i=0 and increment through the string. As we do so, we set another variable lastIdx =len(str)-1.

  • If string i is not equal to string lastIdx – i, then return false, else return true that the string is palindrome.

  • Finally print the result that if a given string is palindrome or not using fmt.Printf () function which formats according to a format specifier and writes to standard output.

Conclusion

In the above two examples we have successfully compiled and executed the Golang program code to check for palindrome.

In the first example we have shown if a given number is palindrome or not using a single function.

In the second example we have shown if a given string is a palindrome or not.

We have used for loop in both the examples. We have used a simple for loop in example 1 and Simple range in for loop in example 2.

Updated on: 16-Nov-2022

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements