题意为,输入“I am zhang.” 输出“zhang. am I”.
我的思路是先将每个单词翻转,然后再讲整个句子翻转。
注意怎么识别单个单词,一种是空格,另一种是句子的结尾。
#include<bits/stdc++.h> using namespace std; void Reverse(string &s, int start, int end) { while(start < end) { char c = s[start]; s[start] = s[end]; s[end] = c; start ++; end--; } } string reverseSentence(string sentence) { int Size = sentence.size(); Reverse(sentence, 0, Size - 1); int start = 0, end = 0; while(start < Size) { if (sentence[start] == ' ') { start ++; end ++; } else if(sentence[end] == ' ' || end == Size) { Reverse(sentence, start, end - 1); start = end; } else { end++; } } return sentence; } int main() { int n; string str; cin>>n; getchar(); for(int i = 1; i <= n; i++) { getline(cin,str); cout<<"Case "<<i<<":"<<reverseSentence(str)<<endl; } return 0; }
原文链接: https://www.cnblogs.com/zhang-zsq/p/12866358.html
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍;
也有高质量的技术群,里面有嵌入式、搜广推等BAT大佬
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/347861
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!