Determine if a string is collapsible: Difference between revisions

no edit summary
(→‎{{header|Ruby}}: Added small intro.)
No edit summary
Line 371:
Final ««« - Hary S Truman»»»
Length : 16
</pre>
 
=={{header|C++}}==
 
<lang cpp>
#include <string>
#include <iostream>
#include <algorithm>
 
template<typename char_type>
std::basic_string<char_type> collapse(std::basic_string<char_type> str)
{
auto i = std::unique(str.begin(), str.end());
str.erase(i, str.end());
return str;
}
 
void test(const std::string& str)
{
std::cout << "original string: <<<" << str << ">>>, length = " << str.length() << '\n';
std::string collapsed(collapse(str));
std::cout << "result string: <<<" << collapsed << ">>>, length = " << collapsed.length() << '\n';
std::cout << '\n';
}
 
int main(int argc, char** argv)
{
test("");
test("\"If I were two-faced, would I be wearing this one?\" --- Abraham Lincoln ");
test("..1111111111111111111111111111111111111111111111111111111111111117777888");
test("I never give 'em hell, I just tell the truth, and they think it's hell. ");
test(" --- Harry S Truman ");
return 0;
}
</lang>
 
{{out}}
<pre>
original string: <<<>>>, length = 0
result string: <<<>>>, length = 0
 
original string: <<<"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln >>>, length = 72
result string: <<<"If I were two-faced, would I be wearing this one?" - Abraham Lincoln >>>, length = 70
 
original string: <<<..1111111111111111111111111111111111111111111111111111111111111117777888>>>, length = 72
result string: <<<.178>>>, length = 4
 
original string: <<<I never give 'em hell, I just tell the truth, and they think it's hell. >>>, length = 72
result string: <<<I never give 'em hel, I just tel the truth, and they think it's hel. >>>, length = 69
 
original string: <<< --- Harry S Truman >>>, length = 72
result string: <<< - Hary S Truman >>>, length = 17
</pre>
 
1,777

edits