C 语言详解 之 单词反转

    技术2025-12-04  3

     

    #include <stdio.h>#include <string.h>#define  MAX_LEN 100int 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;      }

    最新回复(0)