0
0
GoProgramBeginner · 2 min read

Go Program to Check Palindrome String

In Go, you can check if a string is a palindrome by comparing characters from the start and end using a loop, like for i := 0; i < len(s)/2; i++ { if s[i] != s[len(s)-1-i] { return false } }.
📋

Examples

Inputmadam
Outputmadam is a palindrome
Inputhello
Outputhello is not a palindrome
Inputa
Outputa is a palindrome
🧠

How to Think About It

To check if a string is a palindrome, think of reading it from both ends at the same time. Compare the first character with the last, the second with the second last, and so on. If all pairs match, the string is a palindrome; if any pair differs, it is not.
📐

Algorithm

1
Get the input string.
2
Loop from the start to the middle of the string.
3
Compare the character at the current position with the character at the corresponding position from the end.
4
If any characters do not match, return that the string is not a palindrome.
5
If all characters match, return that the string is a palindrome.
💻

Code

go
package main

import (
	"fmt"
)

func isPalindrome(s string) bool {
	for i := 0; i < len(s)/2; i++ {
		if s[i] != s[len(s)-1-i] {
			return false
		}
	}
	return true
}

func main() {
	input := "madam"
	if isPalindrome(input) {
		fmt.Println(input, "is a palindrome")
	} else {
		fmt.Println(input, "is not a palindrome")
	}
}
Output
madam is a palindrome
🔍

Dry Run

Let's trace the string "madam" through the palindrome check.

1

Start loop

i = 0, compare s[0] = 'm' with s[4] = 'm' - they match

2

Next iteration

i = 1, compare s[1] = 'a' with s[3] = 'a' - they match

3

Loop ends

Reached middle of string, all pairs matched, return true

is[i]s[len(s)-1-i]Match?
0mmYes
1aaYes
💡

Why This Works

Step 1: Compare characters from ends

The code compares characters starting from the first and last positions moving inward using s[i] and s[len(s)-1-i].

Step 2: Stop early if mismatch

If any pair of characters does not match, the function immediately returns false, saving time.

Step 3: Return true if all match

If the loop finishes without mismatches, the string is a palindrome, so the function returns true.

🔄

Alternative Approaches

Reverse and compare
go
package main

import (
	"fmt"
)

func isPalindrome(s string) bool {
	runes := []rune(s)
	for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 {
		runes[i], runes[j] = runes[j], runes[i]
	}
	return s == string(runes)
}

func main() {
	input := "madam"
	if isPalindrome(input) {
		fmt.Println(input, "is a palindrome")
	} else {
		fmt.Println(input, "is not a palindrome")
	}
}
This method reverses the string and compares it to the original. It is simpler but uses extra memory for the reversed string.
Recursive check
go
package main

import (
	"fmt"
)

func isPalindromeRecursive(s string, start, end int) bool {
	if start >= end {
		return true
	}
	if s[start] != s[end] {
		return false
	}
	return isPalindromeRecursive(s, start+1, end-1)
}

func main() {
	input := "madam"
	if isPalindromeRecursive(input, 0, len(input)-1) {
		fmt.Println(input, "is a palindrome")
	} else {
		fmt.Println(input, "is not a palindrome")
	}
}
This uses recursion to compare characters from ends inward. It is elegant but uses call stack memory.

Complexity: O(n) time, O(1) space

Time Complexity

The loop runs up to half the string length, so time is proportional to the string size, O(n).

Space Complexity

The check uses no extra space besides variables, so space is O(1).

Which Approach is Fastest?

The direct character comparison loop is fastest and uses least memory compared to reversing or recursion.

ApproachTimeSpaceBest For
Direct loop comparisonO(n)O(1)Fastest and memory efficient
Reverse and compareO(n)O(n)Simple but uses extra memory
Recursive checkO(n)O(n)Elegant but uses call stack
💡
Use a loop comparing characters from both ends to check palindrome efficiently.
⚠️
Beginners often forget to stop the loop at the middle, causing unnecessary checks or errors.