how to perform reversing a sentence Word by Word in C?

后端 未结 12 1349
伪装坚强ぢ
伪装坚强ぢ 2021-01-25 04:49
#include 

int main(void)
{
  int i,j;
  int wordstart = -1;
  int wordend = -1;
  char words[]= \"this is a test\";
  char temp;

  // Reverse each word
         


        
12条回答
  •  无人及你
    2021-01-25 05:12

    Start tokenizing the line from the last character and continue to the first character. Keep one pointer anchored at the base of the current word, and another pointed which will decrease while a word start is not found. When you find a word start while scanning like this, print from the word start pointer to the word end anchor. Update the word end anchor to the previous character of the current word start char.

    You might want to skip the blankspace characters while scanning.

    UPDATE

    This is a quick implementation:

    #include 
    #include 
    #include 
    
    #define MAX_BUF 256
    
    void show_string (char *str, int i, int n)
    {
      while (i <= n)
      {
        printf ("%c", str[i]);
        i++;
      }
    }
    
    int main (void)
    {
      char str[MAX_BUF];
      int end_anchor, start_ptr;
      int state;
    
      printf ("\nEnter a string: ");
      scanf (" %[^\n]", str);
    
      start_ptr = strlen (str) - 1;
    
      end_anchor = start_ptr;
      state = 0;
      while (start_ptr >= -1)
      {
        switch (state)
        {
          case 0:
                 if ((!isspace (str[start_ptr]) && (start_ptr >= 0)))
                 {
                   start_ptr--;
                 }
                 else
                 {
                   state = 1;
                 }
                 break;
    
          case 1:
                 show_string (str, start_ptr + 1, end_anchor);
                 state = 2;
                 start_ptr--;
                 printf (" ");
                 break;
    
          case 2:
                 if (!isspace (str[start_ptr]))
                 {
                   state = 0;
                   end_anchor = start_ptr;
                 }
                 else
                 {
                   start_ptr--;
                 }
                 break;
        }
      }
    
    
      printf ("\n");
      return 0;
    }
    

    The end_anchor points to each end word, and the start_ptr finds the start of the word of which the end is held by end_anchor. When we find a word start (by blankspace characters or start_ptr = -1), we print all the characters from start_ptr + 1 to end_anchor. The + 1 is because of the implementation: start_ptr points to the blankspace character, and the print routine will print all the characters from i to n. Once we have detected one blank space we print it and we skip adjacent blankspaces (in case 2) and preserve only one which is manually printed. Once a non blankspace is detected, we have got another word end, for which we set the end_anchor to this index in the case 2, and set state = 0 , so that we can search for the word start again.

提交回复
热议问题