I have been working on a problem that asks me to a write recursive function that can reverse the words in a string. For example if the string is cat is running, then the result should be running is cat.I already completed the problem, but I feel like my implementation is really bad. Does anyone know any other way to solve this problem? Are there other ways to improve my code?#include <stdio.h>#include <string.h>void reverse(char* string, int count);int main(){   char string[] = cat is running;   //result will be running is cat   reverse(string, -1);   printf(%s\n, string);}void reverse(char* string, int count) {   if (count == 0) {       return;   }   int first = 0;   int second = 0;   int length = 0;   for (int i = 0; i < strlen(string); i++) {       if(string[i] == ' ' && first == 0) {           first = i;           length++;           if (length == count) {              break;           }       } else if(string[i] == ' ') {           second = first+1;           first = i;           length++;           if (length == count) {               break;           }       }   }   if(first == 0 && second == 0) {       return;   }   int flag = 0;    for(int i = first; i >= 0; i--) {       char temp = string[i];       for(int k = i; k < strlen(string) - 1; k++) {           if(flag > 2 && k == (strlen(string) - flag)) {               k = strlen(string);           } else {               string[k] = string[k+1];           }        }    if(i == second) {        i = 0;    }    if (flag == 0) {        string[(strlen(string) - 1) - flag] = temp;        flag++;    } else {        string[(strlen(string) - flag)] = temp;        flag++;    }}count = length;reverse(string, count - 1);}
Recursive function that reverse the words in a string
c;strings;recursion
There is really no need to complicate matters. Keep it as simple as possible.All you need is to track the word delimiter, in your case it is space and of course the null byte.#include <stdio.h>#include <string.h>#include <stdlib.h>char *reverse(char *string) {    char *delim=string;    while(*delim && *delim!=' '){++delim;}; // <-- this makes delim point to the next word    if(*delim){  // <-- do this block only if there a next word        char *retval=malloc(strlen(string)+1);        *(delim++)='\0';        char *temp=reverse(delim);        sprintf(retval, %s %s, temp, string); // <-- move the first word to the end of the sentence        free(temp);  // <--  release the temporary storage to avoid serious memory leak        return retval; // <-- return the newly constructed sentence    }    return strdup(string);  // <-- for all others return a copy of the only word or a copy of an empty string.}int main(int argc, char *argv[]){    char sentence[] = cat is running;    char *a = reverse(sentence);    printf('%s'\n,a);    free(a);    return 0;}Notice the line sprintf(retval,%s %s, reverse(delim),string);? This line will print the string returned by the recursive call to reverse() containing the remaining words in reverse and the first word into retval which is then returned to the previous caller.