How to resolve the algorithm Determine if a string is collapsible step by step in the C++ programming language
How to resolve the algorithm Determine if a string is collapsible step by step in the C++ programming language
Table of Contents
Problem Statement
Determine if a character string is collapsible. And if so, collapse the string (by removing immediately repeated characters).
If a character string has immediately repeated character(s), the repeated characters are to be deleted (removed), but not the primary (1st) character(s).
An immediately repeated character is any character that is immediately followed by an identical character (or characters). Another word choice could've been duplicated character, but that might have ruled out (to some readers) triplicated characters ··· or more.
{This Rosetta Code task was inspired by a newly introduced (as of around November 2019) PL/I BIF: collapse.}
In the following character string:
Only the 2nd t, e, and l are repeated characters, indicated by underscores (above), even though they (those characters) appear elsewhere in the character string.
So, after collapsing the string, the result would be:
Another example: In the following character string:
The "collapsed" string would be:
Write a subroutine/function/procedure/routine··· to locate repeated characters and collapse (delete) them from the character string. The character string can be processed from either direction.
Show all output here, on this page:
Use (at least) the following five strings, all strings are length seventy-two (characters, including blanks), except the 1st string:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Determine if a string is collapsible step by step in the C++ programming language
The provided C++ code demonstrates a function called collapse
that removes consecutive duplicate characters from a string and some sample usage to test the function. Here's a detailed explanation of the code:
-
collapse
Function Template:- The
collapse
function is a generic template function designed to work with both character and wide character strings (std::string
andstd::wstring
). - It takes a single argument,
str
, which is a string of the corresponding character type. - The function's purpose is to remove consecutive duplicate characters from the input string.
- The
-
Implementation of
collapse
:- It uses the
std::unique
algorithm to find and eliminate consecutive duplicate characters in the input string. std::unique
takes an input iterator range and returns an iterator pointing to the end of the unique elements in that range.- The
erase
method is then used to remove the remaining duplicate characters from the end of the string.
- It uses the
-
test
Function:- The
test
function is used to demonstrate the functionality of thecollapse
function. - It takes a single argument,
str
, which is a constant string literal. - The function prints out the original string, its length, and the result of collapsing it using the
collapse
function.
- The
-
Main Function:
- The
main
function is the entry point of the program. - It calls the
test
function with different string literals to demonstrate thecollapse
function's behavior on various input strings.
- The
Example Outputs:
-
Input String: ""
- Collapsed String: ""
-
Input String: ""If I were two-faced, would I be wearing this one?" --- Abraham Lincoln "
- Collapsed String: ""If I wer faced, woud I be wear this on?" --- Abraham Lincoln "
-
Input String: "..1111111111111111111111111111111111111111111111111111111111111117777888"
- Collapsed String: ".1178"
-
Input String: "I never give 'em hell, I just tell the truth, and they think it's hell. "
- Collapsed String: "I never give 'em hell, I just tell the truth, and they think it's hell. " (No duplicates)
-
Input String: " --- Harry S Truman "
- Collapsed String: "--- Harry S Truman " (Removed extra spaces)
Source code in the cpp programming language
#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;
}
You may also check:How to resolve the algorithm Terminal control/Cursor positioning step by step in the Scala programming language
You may also check:How to resolve the algorithm Pentomino tiling step by step in the Java programming language
You may also check:How to resolve the algorithm Conditional structures step by step in the PHP programming language
You may also check:How to resolve the algorithm JortSort step by step in the K programming language
You may also check:How to resolve the algorithm Miller–Rabin primality test step by step in the Prolog programming language