您的位置:首页 > 编程语言

【原创】编程题练习:反转字符串中的单词

2013-04-26 16:54 274 查看
比较老的一道题了,练练手,面向过程的coding

1 #include<iostream>
2
3 using namespace std;
4 int main()
5 {
6 string str = "i am a student.";
7 int i= 0,j = str.length()-1;
8 char temp;
9 while(i < j)
10 {
11 temp = str[i];
12 str[i] = str[j];
13 str[j] = temp;
14 i++;
15 j--;
16 }
17 i = 0;j=0;
18 int temp1,temp2;
19 while(j<str.length())
20 {
21 if(str[j] != ' '&& j != str.length()-1)
22 {
23 j++;
24 continue;
25 }
26 else if(str[j] == ' ')
27 {
28 temp1 = i;
29 j -= 1;
30 temp2 = j;
31 while(i<j)
32 {
33 temp = str[i];
34 str[i] = str[j];
35 str[j] = temp;
36 i++;
37 j--;
38 }
39 i = temp2+2;
40 j = temp2+2;
41 }
42 if(j == str.length()-1)
43 {
44 while(i<j)
45 {
46 temp = str[i];
47 str[i] = str[j];
48 str[j] = temp;
49 i++;
50 j--;
51 }
52 j = str.length();
53 }
54
55 }
56
57 cout << str << endl;
58
59 system("pause");
60
61 return 0;
62
63 }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: