Skip to main content

Reverse string using recursion in golang

Go program for Reverse string using recursion. Here problem description and other solutions.

// Reverse string using recursion in Go
package main
import "fmt"

// This is reversing the string elements recursively
func reverseText(text string, location int) string {
	// Base condition to stop the recursion process
	if location >= 0 {
		// Recursive function call
		return string(text[location]) + reverseText(text, location - 1)
	}
	// When no character remaining
	return ""
}
// This is handling the request process of reverse string elements
func reverse(text string) string {
	// Display given string elements
	fmt.Println("Before Text : [", text, "]")
	return reverseText(text, len(text) - 1)
}
func main() {

	var text string = "ABCDE"
	text = reverse(text)
	// After reverse	
	fmt.Println("After Text  : [", text, "]\n")
	text =reverse("654A321")
	// After reverse	
	fmt.Println("After Text  : [", text, "]\n")
}

Output

Before Text : [ABCDE]
After Text  : [EDCBA]

Before Text : [654A321]
After Text  : [123A456]




Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment