Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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 for?loop. 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
<div class="execute"></div><div class="code-mirror language-java" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token comment">// GOLANG PROGRAM TO CHECK FOR PALINDROME</span>
<span class="token comment">// fmt package allows us to print anything on the screen.</span>
<span class="token keyword">package</span> <span class="token namespace">main</span>
<span class="token keyword">import</span> <span class="token string">"fmt"</span>
<span class="token comment">// start the function main ()</span>
func <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Println</span><span class="token punctuation">(</span><span class="token string">"Golang program to check for palindrome"</span><span class="token punctuation">)</span>
<span class="token comment">// declare the variables</span>
<span class="token keyword">var</span> number<span class="token punctuation">,</span>rem<span class="token punctuation">,</span>temporary <span class="token keyword">int</span>
<span class="token keyword">var</span> reverse <span class="token keyword">int</span> <span class="token operator">=</span> <span class="token number">0</span>
<span class="token comment">// initialize the number variable</span>
number <span class="token operator">=</span> <span class="token number">45454</span>
temporary<span class="token operator">=</span>number
<span class="token comment">// For Loop used</span>
<span class="token keyword">for</span><span class="token punctuation">{</span>
rem <span class="token operator">=</span> number<span class="token operator">%</span><span class="token number">10</span>
reverse <span class="token operator">=</span> reverse<span class="token operator">*</span><span class="token number">10</span> <span class="token operator">+</span> rem
number <span class="token operator">/=</span> <span class="token number">10</span>
<span class="token keyword">if</span><span class="token punctuation">(</span>number<span class="token operator">==</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
<span class="token keyword">break</span> <span class="token comment">// Break Statement used to exit from loop</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span class="token keyword">if</span><span class="token punctuation">(</span>temporary<span class="token operator">==</span>reverse<span class="token punctuation">)</span><span class="token punctuation">{</span>
<span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Printf</span><span class="token punctuation">(</span><span class="token string">"Number %d is a Palindrome"</span><span class="token punctuation">,</span>temporary<span class="token punctuation">)</span>
<span class="token punctuation">}</span><span class="token keyword">else</span><span class="token punctuation">{</span>
<span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Printf</span><span class="token punctuation">(</span><span class="token string">"Number %d is not a Palindrome"</span><span class="token punctuation">,</span>temporary<span class="token punctuation">)</span>
<span class="token punctuation">}</span>
<span class="token comment">// print the result using fmt.Printf () function</span>
<span class="token punctuation">}</span>
</div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>
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
<div class="execute"></div><div class="code-mirror language-java" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token comment">// GOLANG PROGRAM TO CHECK FOR PALINDROME</span>
<span class="token comment">// fmt package allows us to print anything on the screen.</span>
<span class="token keyword">package</span> <span class="token namespace">main</span>
<span class="token keyword">import</span> <span class="token string">"fmt"</span>
<span class="token comment">// start the function main ()</span>
func <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token comment">// declare and initialize the string</span>
str <span class="token operator">:</span><span class="token operator">=</span> <span class="token string">"MADAM"</span>
<span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Println</span><span class="token punctuation">(</span><span class="token string">"Golang program to check palindrome,\n Given Word ="</span><span class="token punctuation">,</span>str<span class="token punctuation">)</span>
<span class="token comment">// calling the function</span>
<span class="token class-name">Palindrome</span><span class="token punctuation">(</span>str<span class="token punctuation">)</span>
<span class="token comment">// print the result using fmt.Printf () function</span>
<span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Printf</span><span class="token punctuation">(</span><span class="token string">"'%s' is palindrome\n"</span><span class="token punctuation">,</span> str<span class="token punctuation">)</span>
<span class="token punctuation">}</span>
func <span class="token class-name">Palindrome</span><span class="token punctuation">(</span>str string<span class="token punctuation">)</span> bool <span class="token punctuation">{</span>
lastIdx <span class="token operator">:</span><span class="token operator">=</span> <span class="token function">len</span><span class="token punctuation">(</span>str<span class="token punctuation">)</span> <span class="token operator">-</span> <span class="token number">1</span>
<span class="token comment">// using for loop</span>
<span class="token keyword">for</span> i <span class="token operator">:</span><span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator"><</span> lastIdx<span class="token operator">/</span><span class="token number">2</span> <span class="token operator">&&</span> i <span class="token operator"><</span> <span class="token punctuation">(</span>lastIdx<span class="token operator">-</span>i<span class="token punctuation">)</span><span class="token punctuation">;</span> i<span class="token operator">++</span> <span class="token punctuation">{</span>
<span class="token keyword">if</span> str<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">!=</span> str<span class="token punctuation">[</span>lastIdx<span class="token operator">-</span>i<span class="token punctuation">]</span> <span class="token punctuation">{</span>
<span class="token keyword">return</span> <span class="token boolean">false</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span class="token keyword">return</span> <span class="token boolean">true</span>
<span class="token punctuation">}</span>
</div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>
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.
