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 8, 2024
1 parent 47fc66f commit dd9fdc8
Showing 1 changed file with 26 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/// 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"
)

// Função para inverter uma 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() {
// Testando a função reverseString com diferentes strings
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 dd9fdc8

Please sign in to comment.