S SmartDocs
Série: Go go 23 linhas · Atualizado 2026-04-18

strings.go

Go/part1-language/examples/11_testing/strings.go

// Package strs provides tiny string utilities used to demonstrate testing.
package strs

// Reverse returns s with its runes in reverse order. It is rune-safe so it
// works on multi-byte UTF-8 characters such as "héllo".
func Reverse(s string) string {
	rs := []rune(s)
	for i, j := 0, len(rs)-1; i < j; i, j = i+1, j-1 {
		rs[i], rs[j] = rs[j], rs[i]
	}
	return string(rs)
}

// IsPalindrome reports whether s reads the same forwards and backwards.
func IsPalindrome(s string) bool {
	rs := []rune(s)
	for i, j := 0, len(rs)-1; i < j; i, j = i+1, j-1 {
		if rs[i] != rs[j] {
			return false
		}
	}
	return true
}

Artigos relacionados