C Program To Reverse Words In A Given String Last Updated : 02 Dec, 2022 Comments Improve Suggest changes Like Article Like Report Example: Let the input string be "i like this program very much". The function should change the string to "much very program this like i" Examples: Input: s = "geeks quiz practice code" Output: s = "code practice quiz geeks" Input: s = "getting good at coding needs a lot of practice" Output: s = "practice of lot a needs coding at good getting" Recommended: Please solve it on “PRACTICE ” first, before moving on to the solution. Algorithm: Initially, reverse the individual words of the given string one by one, for the above example, after reversing individual words the string should be "i ekil siht margorp yrev hcum".Reverse the whole string from start to end to get the desired output "much very program this like i" in the above example. Below is the implementation of the above approach: C // C program to reverse a string #include <stdio.h> // Function to reverse any sequence // starting with pointer begin and // ending with pointer end void reverse(char* begin,char* end) { char temp; while (begin < end) { temp = *begin; *begin++ = *end; *end-- = temp; } } // Function to reverse words void reverseWords(char* s) { char* word_begin = s; // Word boundary char* temp = s; // Reversing individual words as // explained in the first step while (*temp) { temp++; if (*temp =='\0') { reverse(word_begin,temp - 1); } else if (*temp == ' ') { reverse(word_begin,temp - 1); word_begin = temp + 1; } } // Reverse the entire string reverse(s, temp - 1); } // Driver Code int main() { char s[] = "i like this program very much"; char* temp = s; reverseWords(s); printf("%s", s); return 0; } Outputmuch very program this like i Time Complexity: O(n)Auxiliary Space: O(n) The above code doesn't handle the cases when the string starts with space. The following version handles this specific case and doesn't make unnecessary calls to reverse function in the case of multiple spaces in between. Thanks to rka143 for providing this version. C // C program to implement // the above approach void reverseWords(char* s) { char* word_begin = NULL; // temp is for word boundary char* temp = s; // STEP 1 of the above algorithm while (*temp) { /*This condition is to make sure that the string start with valid character (not space) only*/ if ((word_begin == NULL) && (*temp != ' ')) { word_begin = temp; } if (word_begin && ((*(temp + 1) == ' ') || (*(temp + 1) == ''))) { reverse(word_begin, temp); word_begin = NULL; } temp++; // End of while } // STEP 2 of the above algorithm reverse(s, temp - 1); } Time Complexity: O(n) Another Approach: Please refer complete article on Reverse words in a given string for more details! Comment More infoAdvertise with us Next Article C Program To Reverse Words In A Given String kartik Follow Improve Article Tags : Strings C Programs DSA Microsoft Amazon Adobe Morgan Stanley Goldman Sachs Cisco Paytm Accolite Payu Zoho MAQ Software SAP Labs MakeMyTrip Reverse school-programming Wipro CBSE - Class 11 +16 More Practice Tags : AccoliteAdobeAmazonCiscoGoldman SachsMakeMyTripMAQ SoftwareMicrosoftMorgan StanleyPaytmPayuSAP LabsWiproZohoReverseStrings +12 More Similar Reads C# Program To Reverse Words In A Given String Example: Let the input string be "i like this program very much". The function should change the string to "much very program this like i" Examples: Input: s = "geeks quiz practice code" Output: s = "code practice quiz geeks" Input: s = "getting good at coding needs a lot of practice" Output: s = "p 4 min read C Program to Reverse a String Using Recursion Reversing a string means changing the order of characters in the string so that the last character becomes the first character of the string. In this article, we will learn how to reverse a string using recursion in a C program.The string can be reversed by using two pointers: one at the start and o 1 min read How to Reverse a String in C? In C, a string is a sequence of characters terminated by a null character (\0). Reversing a string means changing the order of the characters such that the characters at the end of the string come at the start and vice versa. In this article, we will learn how to reverse a string in C. Example: Inpu 2 min read C Program to Traverse an Array in Reverse Write a C program to traverse a given array in reverse order that contains N elements.ExamplesInput: arr[] = {2, -1, 5, 6, 0, -3}Output: -3 0 6 5 -1 2Input: arr[] = {4, 0, -2, -9, -7, 1}Output: 1 -7 -9 -2 0 4Different Ways to Traverse an Array in Reverse Order in CWe can traverse/print the array in 2 min read Reverse Number Program in C The reverse of a number of means reversing the order of digits of a number. In this article, we will learn how to reverse the digits of a number in C language. Example:Input: 12354Output: 45321Explanation: The number 12354 when reversed gives 45321Input: 623Output: 326Explanation: The number 623 whe 2 min read C Program to Sort a String of Characters Sorting a string of characters refers to the process of rearranging all the characters in the given order. In this article, we will learn how to sort a string of characters using the C program.The most straightforward method to sort a string of characters is by using the qsort() function. Let's take 3 min read C program to reverse the content of the file and print it Given a text file in a directory, the task is to print the file content backward i.e., the last line should get printed first, 2nd last line should be printed second, and so on.Examples: Input: file1.txt has: Welcome to GeeksforGeeks Output: GeeksforGeeks to WelcomeGeeksforGeeks Input: file1.txt has 3 min read Reverse String in C In C, reversing a string means rearranging the characters such that the last character becomes the first, the second-to-last character becomes the second, and so on. In this article, we will learn how to reverse string in C.The most straightforward method to reverse string is by using two pointers t 3 min read C Program to Check for Palindrome String A string is said to be palindrome if the reverse of the string is the same as the string. In this article, we will learn how to check whether the given string is palindrome or not using C program.The simplest method to check for palindrome string is to reverse the given string and store it in a temp 4 min read Like