Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- using namespace std;
- int main()
- {
- string s, s1 = "", s2 = ""; getline(cin, s);
- // v1
- for (size_t i = 0; i < s.length(); ++i)
- {
- if ((s[i] != ' ') || ((s[i] == ' ') && (s1 != "") && (s1[s1.length()-1] != ' '))) s1 += s[i];
- }
- // more readable v2
- for (auto c : s)
- {
- if ((c != ' ') || ((c == ' ') && (s2 != "") && (s2.back() != ' '))) s2 += c;
- }
- cout << "Input string: " << s << endl << "Output string v1: " << s1 << endl << "Output string v2: " << s2 << endl;
- return 0;
- }
Add Comment
Please, Sign In to add comment