-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add reverse_word_string.go to string_algorithms
- Loading branch information
1 parent
47fc66f
commit 0e728a1
Showing
1 changed file
with
27 additions
and
0 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
code/string_algorithms/src/reverse_word_string/reverse_word_string.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |