I need a program to reverse the words in a string.

Input: My car is fast
Output: fast is car My

int printRword(char * line) {
    for(; *line; line++) {
        if(*line == ' ') {
            printRword(line + 1); 
            printf("%s", line); 
            return 0; // after you find the space set it to null 
        }
    }
}

int main(void) {
    char *line = "this is a long line that we are working with\n";
    printf("%s", line);
    printRword(line); 

    return 0;
}

I know I need to set space to null after I find it, and I've tried printRword(line + 1) = '\0'; and that doesn't work any suggestions?

有帮助吗?

解决方案 2

Find the modified working code:

int printRword(char * line)

{

   char tempbuf[100]; //Here length i have hardcoded to 100

   char *ptr;
   strcpy(tempbuf,line); //copied to tempbuf to keep the original string unmodified

   //Replace the \n with the null character
    ptr = strrchr(tempbuf,'\n');
    if(ptr != NULL)
    {
      *ptr = '\0'; 
    }

    while(*tempbuf != '\0')
    {
        ptr = strrchr(tempbuf,' ');
        if(NULL != ptr)
        {
             *ptr = '\0';
              ptr++;

              printf("%s ",ptr); 
         }
         else
         {
             printf("%s\n",tempbuf);
             *tempbuf ='\0';
          }
    }  

}

test result:

atharv@atharv-Inspiron-5423:~/Programming$ ./a.out

this is a long line that we are working with

with working are we that line long a is this

atharv@atharv-Inspiron-5423:~/Programming$

其他提示

You could reverse the whole string, and then reverse each individual word, having the effect of reversing the order of the words but leaving the letters in each word in the correct order. Not the most efficient, perhaps, but conceptually clean -- and not language dependent!

You could go through the string character-by-character, replacing the spaces by ASCII NUL characters (C's string terminators), and recording the next position in each case (by pushing onto a stack), thus recording the beginning of each word. When you get to the end of the string, you can then go backwards through the list of “start-of-word” positions (perhaps by popping off the stack), printing out the word each time followed by a space.

This is the basic idea. If you have to handle multiple spaces between words or newlines, it gets a little bit more complicated, but not much.

I modified your code using the same recursive approach to get the desired output, just added a function that would print only till next space.. there must be a function for this already but i am not aware of it.

#include <stdio.h>


void printTillNextSpace(char *s){
    while(*s != ' ' && *s != '\0' && *s != '\n')
        printf("%c",*s++);
        printf("%c",' ');        
}

int printRword(char * line){

    char* start = line;
    for(;*line; line++){
        if(*line == ' '){
            printRword(line + 1);
            printTillNextSpace(start);
            start = line + 1; 
            return 0; // after you find the space set it to null 
        }
    }
    printTillNextSpace(start);
}

int main(){

    char * line = "this is a long line that we are working with\n";

    printf("%s", line);

    printRword(line); 

return 0;
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top