#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
string reverse(const string& s, const char c)
{
string str = s;
string ret = "";
int pos = 1;
while (pos > 0)
{
pos = str.find(c);
if(pos > 0)
{
string s1 = str.substr(0, pos);
reverse(s1.begin(), s1.end());
ret += s1;
ret += ";";
str = str.substr(pos+1);
}
}
reverse(str.begin(), str.end());
ret += str;
return ret;
}
int main()
{
string s = "we;tonight;you";
cout << reverse("we;tonight;you", ';') << endl;
cout << reverse("abcde;", ';') << endl;
cout << reverse(";", ';') << endl;
cout << reverse("", ';') << endl;
return 0;
}
#if 0
cout << reverse("", ';') << endl; // 输出:空字符串
cout << reverse(";", ';') << endl; // 输出:;
cout << reverse("abcde;", ';') << endl; // 输出:edcba;
cout << reverse("we;tonight;you", ';') << endl; // 输出:ew;thginot;uoy
#endif
以分号分割反转字符串_作业
最新推荐文章于 2021-07-21 20:39:25 发布