Golang Program to Reverse a Sentence using Recursion


In this tutorial, we will learn how to reverse a sentence using recursion in Go Programming Language.

A Recursion is where a function calls itself by direct or indirect means. Every recursive function has a base case or base condition which is the final executable statement in recursion and halts further calls. The recursion continues until some condition is met to prevent it.

Below are two examples showing the two different type of recursion: direct and indirect.

Reverse a Sentence using Recursion by using Direct Recursion Method

Syntax

Func recursion() {
   recursion(); /* function calls itself */
}
func main() {
   recursion();
}

Algorithm

  • Step 1 − Import the package fmt

  • Step 2 − Create the function reversesentence ()

  • Step 3 − Use the if condition to execute the code

  • Step 4 − Recursive call of the function itself

  • Step 5 − Start the function main()

  • Step 6 − call the function reversesentence ()

  • Step 7 − Print the result using fmt.Print().

Example

Golang Program code to Reverse a Sentence using Recursion by using Direct Recursion Method.

// GOLANG PROGRAM TO REVERSE A SENTENCE USING RECURSION package main // fmt package provides the function to print anything import "fmt" // create the function reversesentence() func reversesentence(input string) { if len(input) == 0 { return } // recursive call of the function itself reversesentence(input[1:]) fmt.Print(string(input[0])) } func main() { fmt.Println("GOLANG PROGRAM TO REVERSE A SENTENCE USING RECURSION") // calling the function reversesentence() fmt.Println("Entered sentence =") var sentence string sentence = "Taylor Swift is the best" fmt.Println(sentence) reversesentence(sentence) // Print the result }

Output

GOLANG PROGRAM TO REVERSE A SENTENCE USING RECURSION
Entered sentence =
Taylor Swift is the best
tseb eht si tfiwS rolyaT

Description of the Code

  • In the above program, we first declare the package main

  • We imported the fmt package that includes the files of package fmt

  • Next we create a function reversesentence () to reverse a sentence using recursion technique

  • We will use an if conditional statement which allows you to execute one block of code and if it is false then recursive call to the function itself

  • Now start the function main()

  • Now calling the reversesentence() function

  • And finally printing the reverse sentence on the screen using fmt.Print().

Reverse a Sentence using Recursion by using Indirect Recursion Method

Syntax

func recursion_1() {
   recursion_2()
}
func recursion_2(){
   recursion_1()
}
func main() {
   recursion_1();
}

Algorithm

  • Step 1 − Import the package fmt

  • Step 2 − Create the function reverse ()

  • Step 3 − Use the if condition to execute the code

  • Step 4 − Recursive call of the function reverse2()

  • Step 5 − Create the function reverse2()

  • Step 6 − Recursive call to the function reverse() indirectly)

  • Step 7 − Start the function main()

  • Step 8 − Call the function reverse()

  • Step 9 − Print the result on the screen using fmt.Print()

Example

Golang Program code to Reverse a Sentence Using Recursion by using Indirect Recursion Method

// GOLANG PROGRAM TO REVERSE A SENTENCE USING RECURSION package main // fmt package provides the function to print anything import "fmt" // create the function reverse() func reverse(input string) { if len(input) == 0 { return } // recursive call of the function reverse2 reverse2(input[1:]) fmt.Print(string(input[0])) } func reverse2(n string) { if len(n) == 0 { return } // recursive call of the function the first function indirectly reverse(n[1:]) fmt.Print(string(n[0])) } func main() { fmt.Println("GOLANG PROGRAM TO REVERSE A SENTENCE USING RECURSION") // calling the function reverse() var sentence string sentence = "Golang Solutions" fmt.Println("Entered Sentence\n",sentence) reverse2(sentence) // Print the result }

Output

GOLANG PROGRAM TO REVERSE A SENTENCE USING RECURSION
Entered Sentence
Golang Solutions
snoituloS gnaloG

Description of the Code

  • In the above program, we first declare the package main

  • We imported the fmt package that includes the files of package fmt

  • Next we create a function reverse () to reverse a sentence using recursion technique

  • We will use an if conditional statement which allows you to execute one block of code and if it is false then recursive call to the second function reverse2()

  • Next we create a function reverse2().Here recursive function call to the first function is made which calls the first function reverse () indirectly

  • Now start the function main().GO program execution starts with the function main()

  • Next we call the reverse() function to reverse a sentence.

  • And finally printing the reverse of the sentence on the screen using in-built function fmt.Print().This function is defined under the fmt package and it helps to write standard output.

Conclusion

In the above two examples we have successfully compiled and executed the Golang program code to reverse a sentence using recursion technique. We have shown direct and indirect type of recursion method. In the first example we have shown direct recursion method and in the second example we have shown indirect recursion method.

Updated on: 25-Oct-2022

425 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements