Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <vector>
- #include <sstream>
- #include <cctype>
- #include <map>
- #include <algorithm>
- using namespace std;
- bool is_punctuation(int ch)
- {
- string punct_marks{ ".,!?;:-" };
- return punct_marks.find(ch) != string::npos;
- }
- int main()
- {
- string line;
- getline(cin, line);
- using WordInfo = pair<string, size_t>;
- using WordData = vector<WordInfo>;
- using WordMap = map<int, WordData>;
- WordMap data;
- istringstream iss(line);
- string s;
- while (iss >> s)
- {
- size_t pos;
- if(iss.good())
- pos = static_cast<size_t>(iss.tellg());
- else
- pos = line.length();
- pos -= s.length();
- s.erase(find_if(s.begin(), s.end(), [](int ch) { return is_punctuation(ch); }), s.end());
- data[s.length()].push_back(make_pair(s, pos));
- }
- string result{ line };
- for (auto w : data)
- {
- if (w.second.size() > 1)
- {
- for (size_t fwd = 0, bwd = w.second.size() - 1; fwd < bwd; ++fwd, --bwd)
- {
- string left_str = w.second[fwd].first, right_str = w.second[bwd].first;
- if (left_str != right_str)
- {
- size_t len = w.first;
- result.replace(w.second[fwd].second, len, right_str);
- if (isupper(left_str[0]))
- left_str[0] = tolower(left_str[0]);
- result.replace(w.second[bwd].second, len, left_str);
- }
- }
- }
- }
- result[0] = toupper(result[0]);
- cout << result << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment