top of page

C Program to reverse words in the given String

Writer's picture: Prateek ChauhanPrateek Chauhan

How to write a C Program to Reverse Order of Words in a String with an example.


Implementation:

#include <stdio.h>
#include <string.h>

int main()
{
  	char str[100];
  	int i, len;

  	printf("\n Please Enter any String :  ");
  	gets(str);

  	len = strlen(str);
  	printf("\n ####  Given String in Reverse Order  #### \n");
 
  	for(i = len - 1; i >= 0; i--)
	{
		if(str[i] == ' ')
		{
			str[i] = '\0';
			printf("%s ", &(str[i]) + 1);
		}
	}
	printf("%s", str);

  	return 0;
}
Output:
Please Enter any String: We love Programmersdoor
####  Given String in Reverse Order  ####
Programmersdoor love We
 

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Follow Programmers Door for more.


Comments


bottom of page