Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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.
<div class="execute"></div><div class="code-mirror language-java" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token comment">// GOLANG PROGRAM TO REVERSE A SENTENCE USING RECURSION</span>
<span class="token keyword">package</span> <span class="token namespace">main</span>
<span class="token comment">// fmt package provides the function to print anything</span>
<span class="token keyword">import</span> <span class="token string">"fmt"</span>
<span class="token comment">// create the function reversesentence()</span>
func <span class="token function">reversesentence</span><span class="token punctuation">(</span>input string<span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">if</span> <span class="token function">len</span><span class="token punctuation">(</span>input<span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token number">0</span> <span class="token punctuation">{</span>
<span class="token keyword">return</span>
<span class="token punctuation">}</span>
<span class="token comment">// recursive call of the function itself</span>
<span class="token function">reversesentence</span><span class="token punctuation">(</span>input<span class="token punctuation">[</span><span class="token number">1</span><span class="token operator">:</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
<span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Print</span><span class="token punctuation">(</span><span class="token function">string</span><span class="token punctuation">(</span>input<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
func <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Println</span><span class="token punctuation">(</span><span class="token string">"GOLANG PROGRAM TO REVERSE A SENTENCE USING RECURSION"</span><span class="token punctuation">)</span>
<span class="token comment">// calling the function reversesentence()</span>
<span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Println</span><span class="token punctuation">(</span><span class="token string">"Entered sentence ="</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> sentence string
sentence <span class="token operator">=</span> <span class="token string">"Taylor Swift is the best"</span>
<span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Println</span><span class="token punctuation">(</span>sentence<span class="token punctuation">)</span>
<span class="token function">reversesentence</span><span class="token punctuation">(</span>sentence<span class="token punctuation">)</span>
<span class="token comment">// Print the result</span>
<span class="token punctuation">}</span>
</div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>
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
<div class="execute"></div><div class="code-mirror language-java" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token comment">// GOLANG PROGRAM TO REVERSE A SENTENCE USING RECURSION</span>
<span class="token keyword">package</span> <span class="token namespace">main</span>
<span class="token comment">// fmt package provides the function to print anything</span>
<span class="token keyword">import</span> <span class="token string">"fmt"</span>
<span class="token comment">// create the function reverse()</span>
func <span class="token function">reverse</span><span class="token punctuation">(</span>input string<span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">if</span> <span class="token function">len</span><span class="token punctuation">(</span>input<span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token number">0</span> <span class="token punctuation">{</span>
<span class="token keyword">return</span>
<span class="token punctuation">}</span>
<span class="token comment">// recursive call of the function reverse2</span>
<span class="token function">reverse2</span><span class="token punctuation">(</span>input<span class="token punctuation">[</span><span class="token number">1</span><span class="token operator">:</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
<span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Print</span><span class="token punctuation">(</span><span class="token function">string</span><span class="token punctuation">(</span>input<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
func <span class="token function">reverse2</span><span class="token punctuation">(</span>n string<span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">if</span> <span class="token function">len</span><span class="token punctuation">(</span>n<span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token number">0</span> <span class="token punctuation">{</span>
<span class="token keyword">return</span>
<span class="token punctuation">}</span>
<span class="token comment">// recursive call of the function the first function indirectly</span>
<span class="token function">reverse</span><span class="token punctuation">(</span>n<span class="token punctuation">[</span><span class="token number">1</span><span class="token operator">:</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
<span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Print</span><span class="token punctuation">(</span><span class="token function">string</span><span class="token punctuation">(</span>n<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
func <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Println</span><span class="token punctuation">(</span><span class="token string">"GOLANG PROGRAM TO REVERSE A SENTENCE USING RECURSION"</span><span class="token punctuation">)</span>
<span class="token comment">// calling the function reverse()</span>
<span class="token keyword">var</span> sentence string
sentence <span class="token operator">=</span> <span class="token string">"Golang Solutions"</span>
<span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Println</span><span class="token punctuation">(</span><span class="token string">"Entered Sentence\n"</span><span class="token punctuation">,</span>sentence<span class="token punctuation">)</span>
<span class="token function">reverse2</span><span class="token punctuation">(</span>sentence<span class="token punctuation">)</span>
<span class="token comment">// Print the result</span>
<span class="token punctuation">}</span>
</div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>
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.
