/*==================================================
單詞倒排
總時間限制: 1000ms 記憶體限制: 65536kB
描述
編寫程式,讀入一段英文(英文中不包含标點),
将所有單詞的順序倒排并輸出,其中單詞以空格分隔。
輸入
輸入為一個字元串(字元串長度最大為100)
輸出
輸出為按要求排續後的字元串
樣例輸入
I am a student
樣例輸出
student a am I
注:這個代碼是覃宗華寫的。
====================================================*/
1 #include<iostream>
2 #include<cstdio>
3 #include<cstring>
4 using namespace std;
5 int main()
6 {
7 char a[500] = {0};
8 int n, i, j;
9 freopen("5.in","r",stdin);
10 while(gets(a))
11 {
12 n = strlen(a);
13 for(i=n-1;i>=0;i--)
14 {
15 if(a[i]==' ')
16 {
17 for(j=i+1;a[j]!=' '&&a[j]!='\0';j++)
18 cout<< a[j];
19 cout<<" ";
20 }
21 }
22 for(i=0;a[i]!=' '&&a[i]!='\0';i++)
23 cout<<a[i];
24 cout << endl;
25 }
26 return 0;
27 }
1 #include<stdio.h>
2 #include<string.h>
3 int main()
4 {
5 char a[500],temp,b[100];//a數組存貯整個句子,b存儲某一個單詞
6 int i,len,j,k;
7 gets(a);
8 len=strlen(a);
9 for(i=0;i<len/2;i++)//對整個字元串進行翻轉
10 {
11 temp=a[i];
12 a[i]=a[len-1-i];
13 a[len-1-i]=temp;
14 }
15 //printf("\n%s\n",a);
16 i=0;
17 j=0;
18 while(a[i]!='\0')
19 {
20 if(a[i]!=' ')
21 {//掃描句子,遇到非空格字元則存到b數組
22 b[j]=a[i];
23 j++;
24 }
25 else
26 {//掃描句子時遇到空格,逆向輸出b數組存貯的那個被逆序的單詞
27 for(k=j-1;k>=0;k--)
28 {
29 printf("%c",b[k]);
30 }
31 printf(" ");
32 j=0;
33 }
34 i++;
35 }/**/
36 for(k=j-1;k>=0;k--)//最後一個單詞需要單獨處理
37 {
38 printf("%c",b[k]);
39 }
40 printf("\n");
41 return 0;
42 }
轉載于:https://www.cnblogs.com/huashanqingzhu/p/3500465.html