Home / Programs / Strings and recursion
Programming Example

Strings and recursion

👁 720 Views
💻 Practical Program
📘 Step by Step Learning
Strings and recursion

Program Code

#include<stdio.h>
#include<string.h>
void display(char *str);
void Rdisplay(char *str);
int length(char *str);
main( )
{
	char str[100];
	printf("Enter a string : ");
	gets(str);
	
	display( str );

	printf("\n");
	
	Rdisplay(str);
	
	printf("\n");
	printf("%d\n",length(str));
}/*End of main()*/

void display(char *str )
{
	if(*str == '\0')
	return;
	
	putchar(*str );
	display(str+1);
	
}/*End of display()*/


void Rdisplay(char *str )
{
if(*str == '\0')
	return;
	
Rdisplay(str+1);
putchar(*str );
}/*End of Rdisplay()*/


int length(char *str )
{
	if(*str == '\0')
	return 0;
	
	return (1 + length(str+1));
}/*End of length()*/

Output

Enter a string : atnyla
atnyla
alynta
6
Press any key to continue . . .

Explanation

Strings and recursion

How to learn from this program

First read the algorithm, then study the program code line by line. After that, compare the code with the output and finally go through the explanation. This approach helps learners understand both the logic and the implementation properly.