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

后端 未结 12 1448
伪装坚强ぢ
伪装坚强ぢ 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:00

    if(words[i] != ' ') 
        wordstart = i;
    

    This statement what about the else part? if words[i] == ' ', and wordstart remains -1. So maybe try to use:

    while (words[i] && words[i] == ' ') ++i;
      if (!words[i])
          break;
    wordstart = i;
    

    Then you should output the result out of the i loop. Finally, if you want to get the result you expected, you should reverse the whole sentence once more, with the way you used in the loop.

提交回复
热议问题