Recursion
Computer Science, GATE

Recursion Using C

In c, it is possible for functions to call themselves. A function calling itself is called as recursion sometimes called as ‘circular definition’. It is the process of defining something in terms of itself.

void abc()
{
abc(); //Function abc is calling itself again and again this is recursion
}

int main()
{
abc();
}



Recursion is used in problems like TowerofHanoi, tree traversal like Inorder,Preorder,Postorder etc.

Lets take an example which uses recursion. The example below is classical example for understanding the concept of recursion.

#include<stdio.h>
#include<conio.h>
long rec(int x);
int main()
{
long Factorial=1;
int number=1;
printf(“Enter No. to Find Factorial\n”);
scanf(“%d”,&number);
Factorial=rec(number);
printf(“Factorial of %d is %ld\n”,number,Factorial);
return 0;
}
long rec(int x) //Recursive function for calculating factorial
{
int f
if(x==1)
return 1;
else
f=x*rec(x-1); //Here function calling itself
return f;
}

Understanding above Code

This program can be understood as follow for number = 3.

Recursion

When we call function rec for x=3 it check is x equal to 1 no so it goes to else part and perform 3*rec(3-1). Now rec is again called for x=2 it again checks is is equal to 1 no so it goes to else part and perform 2*rec(2-1).Again rec is called for x=1 and finally here x is equal to 1 so it return 0 to the last function i.e., rec(2-1) becomes 1 and then multiply 2 to it goes to last function call i.e., rec(3-1) and it performs 3*2 = 6 and return it to main() hence function terminates here.

So we have seen here how a function call itself again and again untill a terminating condition is met here that condition is x equal to 1 or not once x becomes equal to 1 it backtracks to last function call.



Recursion uses stack because here the function which was called first was executed at the end i.e., in FIRST IN LAST OUT manner.

For More on Computer Science Click Here..!!

Reverse
Computer Science

Reverse Strings in C

There are three ways to reverse string in c.They are as follows:

  1. Use predefined function from c library to reverse a string.
  2. Directly print reversed string.
  3. Write own code of reversing a string permanently by swapping.

Let’s see all the program to reverse a given string.

1. Predefined Function

C library has a predefined function called as strrev() that reverses a string. It takes only one argument that is the variable which holds the string to be reversed or the string itself direclty surrounded by double qoutes. Lets have a look at the program.



#include<stdio.h>
#include<string.h> //header file that contain strrev() function
int main()
{
char str[20];
printf(“Enter String to be reversed\n”);
gets(str);
printf(“String is %s\n”,str);
printf(“Reverse String is %d\n”,strrev(str));
return 0;
}

2. Printing Reversed String

We can directly print a string in reversed order.

#include<stdio.h>
#include<string.h>
int main()
{
char str[20];
int i,length;
printf(“Enter a string\n”);
gets(str);
length=strlen(str); //Count the length of string
for(i=length-1;i>=0;i–)
{
printf(“Reversed string is %c”,str[i]);
}
return 0;
}



3. Write own code of reversing string

#include<stdio.h>
#include<conio.h>
int main()
{
char a[20],b[20];
int count=0,i=0,j=1;
printf(“Enter a string to be reversed\n”);
scanf(“%s”,&a);
while(a[count]!=’\0′) /*Calculate length of string*/
{
count++;
}
j=count-1;
while(i<count) /* Reverses string*/
{
b[i]=a[j];
j–;
i++;
}
b[i]=’\0′; /*Terminates string*/
printf(“%s\n”,b);
return 0;
}

Here we would have used strlen() function direclty to count the length of string but here we are reversing string explicitly that’s why we don’t used predefined function strlen to count length of string and counted it too explicitly.



Above all code snippets are for reversing a string if you have any new method then feel free to share and if you found something wrong in above code then let us know in the comment box below.

For study materials of GATE Click Here..!!

For more on computer Science Click Here..!!