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

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

     #include 
     #include    
      using namespace std;
      char* stringrev(char s[], int len)
      {
        char *s1 = (char*)malloc(len+1);
        int i=0;
        while (len>0)
        {
          s1[i++] = s[--len];
        }
       s1[i++] = '\0';
        return s1;   
      }
    
       void sentrev(char s[], int len)
     {
        int i=0; int j=0;
         char *r = (char*)malloc(len+1);
         while(1)
         {
         if(s[j] == ' ' || s[j] == '\0')
         {
           r = stringrev(s+i, j-i);
           i = j+1;
           cout<

    The above code snap reverse the sentence, using char *r and printing cout<

提交回复
热议问题