标签:class turn oid -- code 空格 for lin tchar
题意为,输入“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; }
字符串,翻转(单词本身没有翻转,只是在句子中的位置发生改变)
标签:class turn oid -- code 空格 for lin tchar
原文地址:https://www.cnblogs.com/zhang-zsq/p/12866358.html