How to check if string is palindrome in Go

1 Answer

0 votes
package main

import (
    "fmt"
    "strings"
)

func isPalindrome(s string) bool {
    s = strings.ToLower(s)
    
    length := len(s)
    
    // Check each character from the start and end
    for i := 0; i < length/2; i++ {
        if s[i] != s[length-i-1] {
            return false
        }
    }
    return true
}

func main() {
    s := "abcba"
    
    fmt.Println(isPalindrome(s))
}

 
 
/*
run:
 
true
   
*/

 



answered Dec 24, 2024 by avibootz
...