Home / Programs / Strings and recursion
🚀 Programming Example

Strings and recursion

👁 720 Views
💻 Practical Program
📘 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()*/
                        

🖥 Program Output

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

📘 Explanation

Strings and recursion
📚 Learning Subject

Master Programming Through Practical Examples

Improve your coding logic, problem-solving skills and programming confidence by practicing real-world examples with explanations.

🎯 How to learn from this example

First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.

🔥 Practice suggestion

Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.