Learn basic concept of c , c++ and python programming in regularcodes.com

Spread the post

Reverse a string using recursion

write an efficient algorithm to reverse a string using recursion.

Given array elements

Stack Areamaincounter (int) =0str_data [0] r [1] e [2] p [3] o [4] l [5] e [6] v [7] e [8] d [9] [10] m [11] a [12] [13] I [14] [15] d [16] n [17] a [18] [19] g [20] n [21] i [22] m [23] m [24] a [25] r [26] g [27] o [28] r [29] p [30] [31] e [32] v [33] o [34] l [35] [36] I [37] \0

Output

Before : repoleved ma I dna gnimmargorp evol I

After : I love programming and I am developer


Function: This function are reversing of given string.

This function are accept three parameter.

1) Address of array which are reverse.

2) Integer index value start by zero.

3) One pointer variable,which are point to integer variable and this variable start with 0.

Our goal is to reverse given string.

Beforemaincounter (int) =0str_data [0] r [1] e [2] p [3] o [4] l [5] e [6] v [7] e [8] d [9] [10] m [11] a [12] [13] I [14] [15] d [16] n [17] a [18] [19] g [20] n [21] i [22] m [23] m [24] a [25] r [26] g [27] o [28] r [29] p [30] [31] e [32] v [33] o [34] l [35] [36] I [37] \0 Aftermaincounter (int) =37str_data [0] I [1] [2] l [3] o [4] v [5] e [6] [7] p [8] r [9] o [10] g [11] r [12] a [13] m [14] m [15] i [16] n [17] g [18] [19] a [20] n [21] d [22] [23] I [24] [25] a [26] m [27] [28] d [29] e [30] v [31] e [32] l [33] o [34] p [35] e [36] r [37] \0

Program for Reverse string using recursion. Time complexity O(n).


Output

Visualize process.

Stack Areamaincounter (int) =0str_data [0] r [1] e [2] p [3] o [4] l [5] e [6] v [7] e [8] d [9] [10] m [11] a [12] [13] I [14] [15] d [16] n [17] a [18] [19] g [20] n [21] i [22] m [23] m [24] a [25] r [26] g [27] o [28] r [29] p [30] [31] e [32] v [33] o [34] l [35] [36] I [37] \0reverseauxilary(pointer)data(pointer)index (int) =0info (char) =rreverseauxilary(pointer)data(pointer)index (int) =1info (char) =ereverseauxilary(pointer)data(pointer)index (int) =2info (char) =preverseauxilary(pointer)data(pointer)index (int) =3info (char) =oreverseauxilary(pointer)data(pointer)index (int) =4info (char) =lreverseauxilary(pointer)data(pointer)index (int) =5info (char) ?

Visualize process Try it Yourself

Spread the post

Recommended Posts: