输入任意个单词,实现对单词位置的倒置而单词本身不变,是对“I am from shanghai”的补充。

法一:

  1 # include <stdio.h>

  2 # include <stdlib.h>

  3 # include <string.h>

  4

  5 void inseq(char *p, int n)

  6 {

  7     char *s ;

  8     for(s = p + n - 1;s >= p;s--)

  9     {

 10         if(' ' == *s)

 11         {

 12             *s = '\0';

 13             printf("%s ", s + 1);

 14         }

 15     }

 16     printf("%s", s + 1);

 17     printf("\n");

 18 }

 19

 20 int main()

 21 {

 22     char *str1;

 23     int len;

 24     str1 = (char *)malloc(sizeof(char) * 100);

 25     if(NULL == str1)

 26     {

 27         perror("malloc");

 28     }

 29

 30     printf("Please input a string:\n");

 31     gets(str1);

 32

 33     len = strlen(str1);

 34     inseq(str1, len);

 35

 36     return 0;

 37 }

 

法二:输入结束要输入squit来结束,而squit不计在内。

  1 # include <stdio.h>

  2 # include <stdlib.h>

  3 # include <string.h>

  4

  5

  6 void C (char *s1[], int n)

  7 {

  8     int i = 0;

  9

 10     for(i = n - 1 ;i > 0; i--)

 11     {

 12         printf("%s",s1[i]);

 13     }

 14

 15     printf("%s \n", s1[0]);

 16 }

 17

 18 int main()

 19 {

 20     char* str[1000];

 21     int i = 0;

 22

 23     printf("PLease input:\n");

 24     str[0] = (char *)malloc(sizeof(char) * 30);

 25     gets(str[0]);

 26

 27     while(strcmp(str[i],"squit") != 0)

 28     {

 29         i++;

 30         str[i] = (char *)malloc(sizeof(char) * 30);

 31         gets(str[i]);

 32     }

 33

 34     C(str, i);

 35

 36     for(i ;i>0;i--)

 37     {

 38         free(str[i]);

 39     }

 40

 41     return 0;

 42 }

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值