C 语言详解 之 单词反转

来源:互联网 发布:制作照片软件下载 编辑:程序博客网 时间:2024/05/18 05:16

 

#include <stdio.h>
#include <string.h>
#define  MAX_LEN 100
int main()
{
   char str[MAX_LEN];
   printf("input a string separated by a blank:/n");
   gets(str);
   printf("%s/n",str);
   char *words[MAX_LEN];
   int length = strlen(str);
   int count=0;
   int i=0;
   while(' '== str[i])
      i++;
 
   int begin=i;
   for(int j=i;j<length;j++)
   {
      if(' ' == str[j])
        {
            words[count]= &str[begin];
             count++;
            str[j]='/0';
            begin += (j-begin)+1;
         }
    }

 

     words[count]=&str[begin];
     for(int m=count;m>=0;m--)
        printf("%s ", words[m]);


   return 0;
     
}