Skip to content

Commit 01455f0

Browse files
authored
Merge branch 'zhravan:main' into main
2 parents f1bb909 + 9e81ee4 commit 01455f0

1 file changed

Lines changed: 21 additions & 0 deletions

File tree

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package strings_runes
2+
3+
import "unicode/utf8"
4+
5+
// countRunes returns the number of runes (Unicode code points) in a string.
6+
// Runes represent Unicode characters, which may be multi-byte.
7+
// For example, "hello" has 5 runes, and "你好世界" has 4 runes (not 12 bytes).
8+
func countRunes(s string) int {
9+
return utf8.RuneCountInString(s)
10+
}
11+
12+
// reverseString reverses a string by rune, preserving multi-byte characters.
13+
// It converts the string to a slice of runes, reverses the slice, then converts back.
14+
// This ensures multi-byte Unicode characters like "你" are kept intact.
15+
func reverseString(s string) string {
16+
runes := []rune(s)
17+
for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 {
18+
runes[i], runes[j] = runes[j], runes[i]
19+
}
20+
return string(runes)
21+
}

0 commit comments

Comments
 (0)