Skip to main content
DevTyp.i.ng
Back

Share

Palindrome checkergolang

00:00:00

Press Esc to exit this exercise.

package main

import (
  "fmt"
  "strings"
)

func main() {
  fmt.Println(isPalindrome("Hello olleH"))
  fmt.Println(isPalindrome("Kayak"))
  fmt.Println(isPalindrome("Hello world"))
}

// Define a function named "isPalindrome" that takes a single string argument and returns a boolean value
func isPalindrome(text string) bool {
  // Convert the input string to lowercase to make the palindrome check case-insensitive
  text = strings.ToLower(text)

  // Initialize two pointers, i and j, pointing to the beginning and end of the string
  i, j := 0, len(text)-1

  // Start a loop that continues while i is less than j
  for i < j {
  // Check if the characters at positions i and j in the string are different
  if text[i] != text[j] {
    // If they are different, the string is not a palindrome, so return false
    return false
  }

  // Move the left pointer (i) one step to the right
  i++

  // Move the right pointer (j) one step to the left
  j--
  }

  // If the loop completes without finding any differing characters, the string is a palindrome, so return true
  return true
}