赞
踩
两要素:
1.多余空格的处理
2.读取并更改指定字符串
还得多学习
- #include<iostream>
- #include<algorithm>
- #include<cctype>
- using namespace std;
- string str;
- bool check(int x,int y,int len) //x为单词前一位,y为单词后一位,len为字符串长度
- {
- if((x < 0|| str[x]==' '||ispunct(str[x]))&&(y>=len||str[y]==' '||ispunct(str[y])))
- return true;
- return false;
- }
- int main()
- {
- int T;
- cin >> T;
- getchar();
- while(T--)
- {
- getline(cin,str);
- cout << str << endl;
- int i = 0, j = str.length() - 1;
- //去掉首、尾空格
- while(str[i] == ' ') i++;
- while(str[j] == ' ') j--;
- //处理str字符串
- int cot = 0;
- for(;i <= j;i ++, cot ++) //利用双指针处理多余空格问题
- {
- if(isupper(str[i]) && str[i] != 'I')
- str[cot] = str[i] + 32;
- else if(str[i] == '?')
- str[cot] = '!';
- else if(str[i] == ' ')
- {
- str[cot] = ' ';
- while(str[++i] == ' '); //判断下一个字符是否为空格
- if(ispunct(str[i])) //符号前不需要空格
- cot --;
- i --;
- }
- else // 不修改
- str[cot] = str[i];
- }
- string goal="";
- for(int i = 0;i < cot;i ++)
- {
- if(str[i] == 'I' && check(i - 1,i + 1, cot))
- goal += "you";
- else if(str.substr(i,2) == "me" && check(i - 1, i + 2, cot))
- goal += "you",i += 1;
- else if(str.substr(i,7) == "can you" && check(i - 1,i + 7, cot))
- goal += "I can", i += 6;
- else if(str.substr(i,9) == "could you" && check(i - 1,i + 9, cot))
- goal += "I could",i += 8;
- else goal += str[i];
- }
- goal = "AI: " + goal;
- cout << goal << endl;
- }
- return 0;
- }

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。