Home Strings Print reverse of a string using recursion.

Print reverse of a string using recursion.

by nikoo28
0 comment

Question: Write a program in C to reverse a string using recursion

Input: Hello Everyone
Output: enoyrevE olleH

We will use the simple concept of recursion to print the string in reversed order. Note that, we will not store the reversed string in any variable, we are just printing the string in reversed order.


//defining a function to print a string in reverse order
void reversePrint(char *str)
	// this is the terminating condition of the recursive loop.
	while(*str != '\0')
		// here is the recursive step,
		// we call the string with its next character.

		// once all the recursions are complete,
		// the program returns here and starts printing the
		// characters one by one

int main(void)
	char *s=(char *)malloc(sizeof(char)*100);

	//we use this statement so that spaces are scanned


	return 0;

Recursive function (reversePrint) takes string pointer (str) as input and calls itself with next location to passed pointer (str+1). Recursion continues this way, when pointer reaches ‘\0′, all functions accumulated in stack print char at passed location (str) and return one by one.

Method 2:- Reverse a string without using recursion.

0 comment

You may also like

Enclose codes in [code lang="JAVA"] [/code] tags

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More