将一句话按单词逆转

时间:2021-02-21 15:46:39
  1 #include <stdio.h>
  2 #include <string.h>
  3 
  4 int main()
  5 {
  6         char *p = {"hello luoxang. how are you?"};
  7         char arr[5][20];
  8         char (*r)[20] = arr;
  9         char brr[1024] = {0};
 10         char *pn = brr;     //pn指针是中间方便操作数组的
 11         strcpy(pn, p);      //这里是将p指向的字符串通过pn赋到brr
 12         char *str = NULL;   //str用来存放strstr返回的地址
 13         char *tmp = NULL;   //tmp用来存放strtok返回的地址
 14         int i=0, j=0;
 15 
 16         do{
 17                 str = strstr(pn, " ");
 18                 tmp = strtok(pn, " ");
 19                 pn = str + 1; //通过pn操作数组而不是直接用brr
 20                 strcpy(*(r+i),  tmp); //一定不能写成*(r+i)=tmp
 21                 i++;
 22         }while(str);          //没找到返回0        
 23 
 24         for(j=4; j>=0; j--)
 25                 printf("%s ", *(r+j));
 26 
 27         printf("\n");
 28 
 29 }
~