How to resolve the algorithm Determine if a string is squeezable step by step in the C++ programming language
How to resolve the algorithm Determine if a string is squeezable step by step in the C++ programming language
Table of Contents
Problem Statement
Determine if a character string is squeezable. And if so, squeeze the string (by removing any number of a specified immediately repeated character).
This task is very similar to the task Determine if a character string is collapsible except that only a specified character is squeezed instead of any character that is immediately repeated.
If a character string has a specified immediately repeated character(s), the repeated characters are to be deleted (removed), but not the primary (1st) character(s).
A specified immediately repeated character is any specified 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: squeeze.}
In the following character string with a specified immediately repeated character of e:
Only the 2nd e is an specified repeated character, indicated by an underscore (above), even though they (the characters) appear elsewhere in the character string.
So, after squeezing the string, the result would be:
Another example: In the following character string, using a specified immediately repeated character s:
The "squeezed" string would be:
Write a subroutine/function/procedure/routine··· to locate a specified immediately repeated character and squeeze (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:
Note: there should be seven results shown, one each for the 1st four strings, and three results for the 5th string.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Determine if a string is squeezable step by step in the C++ programming language
The C++ code performs a squeeze operation on a string, removing consecutive occurrences of a specified character while preserving the remaining characters.
Detailed Explanation:
-
Template Function squeeze:
- Parameters: Takes a string (
std::basic_string
) and a character (char_type
). - Purpose: Removes consecutive occurrences of the specified character from the string.
- Implementation:
- Uses
std::unique
to iterate over the string characters. - The lambda function
[ch](char_type a, char_type b) { return a == ch && b == ch; }
checks for consecutive occurrences of the characterch
. - The iterator
i
points to the first character after the last consecutive occurrence. - The string is then truncated to length
i - str.begin()
, effectively removing the consecutive occurrences.
- Uses
- Parameters: Takes a string (
-
Function test:
- Parameters: A string and a character.
- Purpose: Tests the squeeze function for a given string and character.
- Implementation:
- Prints the original string and its length.
- Calls
squeeze
to remove consecutive occurrences of the character. - Prints the squeezed string and its length.
-
Main Function:
- Calls
test
on multiple string and character combinations to demonstrate the squeeze operation. - Tests various cases, such as empty strings, strings with consecutive characters, strings with multiple occurrences of the character, and strings with leading/trailing whitespace.
- Calls
Output:
The output shows the original strings, their lengths, and the squeezed strings after removing consecutive occurrences of the specified character.
For example, the test case:
test("..1111111111111111111111111111111111111111111111111111111111111117777888", '7');
will produce the following output:
character: '7'
original: <<<..1111111111111111111111111111111111111111111111111111111111111117777888>>>, length: 82
result: <<<..1111111111111111111111111111111111111111111111111111111118>>>, length: 49
The original string has a sequence of consecutive '7' characters. After squeezing, only one '7' remains.
Source code in the cpp programming language
#include <algorithm>
#include <string>
#include <iostream>
template<typename char_type>
std::basic_string<char_type> squeeze(std::basic_string<char_type> str, char_type ch) {
auto i = std::unique(str.begin(), str.end(),
[ch](char_type a, char_type b) { return a == ch && b == ch; });
str.erase(i, str.end());
return str;
}
void test(const std::string& str, char ch) {
std::cout << "character: '" << ch << "'\n";
std::cout << "original: <<<" << str << ">>>, length: " << str.length() << '\n';
std::string squeezed(squeeze(str, ch));
std::cout << "result: <<<" << squeezed << ">>>, length: " << squeezed.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", '7');
test("I never give 'em hell, I just tell the truth, and they think it's hell. ", '.');
std::string truman(" --- Harry S Truman ");
test(truman, ' ');
test(truman, '-');
test(truman, 'r');
return 0;
}
You may also check:How to resolve the algorithm Phrase reversals step by step in the Quackery programming language
You may also check:How to resolve the algorithm Execute a system command step by step in the Run BASIC programming language
You may also check:How to resolve the algorithm Text processing/2 step by step in the Phix programming language
You may also check:How to resolve the algorithm Hello world/Newline omission step by step in the Smalltalk programming language
You may also check:How to resolve the algorithm Permutations step by step in the ooRexx programming language