Skip to content

Commit 0e728a1

Browse files
Add reverse_word_string.go to string_algorithms
1 parent 47fc66f commit 0e728a1

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/// Part of Cosmos by OpenGenus Foundation
2+
/// Receives a string and returns the reverse of it
3+
/// Contributed by: Joao Pedro Campos Silva (joaopedrocampos)
4+
5+
package main
6+
7+
import (
8+
"fmt"
9+
)
10+
11+
// Function to reverse a string
12+
func reverseString(s string) string {
13+
runes := []rune(s)
14+
15+
for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 {
16+
runes[i], runes[j] = runes[j], runes[i]
17+
}
18+
19+
return string(runes)
20+
}
21+
22+
func main() {
23+
fmt.Println(reverseString("I know what you did last summer")) // Output: remmus tsal did uoy tahw wonk I
24+
fmt.Println(reverseString("OpenGenus cosmos")) // Output: somsoc suneGnepO
25+
fmt.Println(reverseString("GoLang")) // Output: gnaLoG
26+
fmt.Println(reverseString("12345")) // Output: 54321
27+
}

0 commit comments

Comments
 (0)