Skip to content

Commit

Permalink
Add reverse_word_string.go to string_algorithms
Browse files Browse the repository at this point in the history
  • Loading branch information
joaopedrocampos committed Oct 10, 2024
1 parent 47fc66f commit 0e728a1
Showing 1 changed file with 27 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/// Part of Cosmos by OpenGenus Foundation
/// Receives a string and returns the reverse of it
/// Contributed by: Joao Pedro Campos Silva (joaopedrocampos)

package main

import (
"fmt"
)

// Function to reverse a string
func reverseString(s string) string {
runes := []rune(s)

for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 {
runes[i], runes[j] = runes[j], runes[i]
}

return string(runes)
}

func main() {
fmt.Println(reverseString("I know what you did last summer")) // Output: remmus tsal did uoy tahw wonk I
fmt.Println(reverseString("OpenGenus cosmos")) // Output: somsoc suneGnepO
fmt.Println(reverseString("GoLang")) // Output: gnaLoG
fmt.Println(reverseString("12345")) // Output: 54321
}

0 comments on commit 0e728a1

Please sign in to comment.