1.倒置英文句子中单词的字母顺序
hello,I am good. -> olleh,I ma doog.
#include<iostream> #include<string.h> using namespace std; class Reverse { public: void Reverse_1(string s,string &s2) { if(s.length()<=0) return ; int i=0,j=0; while(s[i]!='\0') { while((s[i]>='a'&&s[i]<'z')||(s[i]>='A'&&s[i]<'Z')) i++; if(s[i]=='.'||s[i]=='!'||s[i]==','||s[i]=='?'||s[i]==' '||s[i]=='\0') { string temp=Reverse_2(s,j,i-1); s2+=temp; s2+=s[i]; if(s[i]=='\0') return; } i++; j=i; } } string Reverse_2(string s,int begin,int end)<span style="font-family: Arial;">//翻转每个单词.</span> { string s1; char temp; int index=begin,index1=end-begin+1; while(begin<=end) { temp=s[begin]; s[begin]=s[end]; s[end]=temp; end--; begin++; } return s.substr(index,index1);; } }; int main() { string str,s; s=""; getline(cin,str); Reverse r; r.Reverse_1(str,s); cout<<s<<endl; }原文链接:https://www.f2er.com/javaschema/284476.html