法一:
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 }