You are attempting a brute force solution for a problem that does not require one. Before taking that approach, try to analyze the problem a little more, to see if you can find a direct solution. The program below is such a solution.
It starts by eliminating all the non-digits. After that, when there are i
characters still remaining to be discovered, it reserves the last i - 1
characters of the digit string, and searches the rest to find the std::max_element
. It begins its search just beyond the place where the last character was found. The std::max_element
found among the characters it searches is the next character in the result.
It is a good idea is to create test cases that will fail, just to make sure your algorithm is robust.
It is also important to have a running program, even when it is only partially complete. At first, function extract_largest_number
simply returned the empty string! I coded the main routine, with its test_strings
, and just that "stub" for function extract_largest_number
. The result was obviously incorrect, but the program compiled and ran.
The next thing I coded was function eliminate_non_digits
, which I knew already was going to be just a couple calls into the <algorithm>
header. At this point, I added the error trapping code to function extract_largest_number
, but nothing more. It simply returned the string after non-digits had been removed. Once again, the result was obviously wrong, but my program still ran. Because it ran, I was able to scan the output, and verify that function eliminate_non_digits
was working correctly.
Finally, with this structure in place, I coded the actual solution, which turned out to be easier than I expected. This method of "stub-programming" often leads to that result.
Your original question states, "the sequence is only 4 numbers," so I took that as a requirement: a valid solution has exactly 4 digits. In case you need to allow shorter sequences, function extract_biggest_number
has a parameter named require_exact_length
. When the corresponding argument is false
, digit sequences of any length are output.
One final tip: as you learn more about C++, try to use functions in the <algorithm>
header as often as you can.
#include <algorithm> // back_inserter, copy_if, max_element
#include <cctype> // isdigit
#include <iostream> // cout
#include <string> // string
#include <vector> // vector
auto eliminate_non_digits(std::string const& s) -> std::string
{
std::string result;
std::copy_if(s.cbegin(), s.cend(), std::back_inserter(result),
[](char const c) {
return std::isdigit(c);
}
);
return result;
}
auto extract_biggest_number(
std::string const& s,
bool const require_exact_length = true,
std::string::size_type const length = 4u
) -> std::string
{
std::string result, digits{ eliminate_non_digits(s) };
if (digits.size() <= length)
result = digits;
else if (length) {
auto d{ digits.cbegin() };
for (auto i{ length }; i--; ) {
d = std::max_element(d, digits.cend() - i);
result.push_back(*d++);
}
}
if (!length)
result = "ERROR: length == 0";
else if (result.empty())
result = "ERROR: input string does not have any digits";
else if (require_exact_length && result.size() < length)
result = "ERROR: input string does not have enough digits";
else
result = '\"' + result + '\"';
return result;
}
int main()
{
auto& log{ std::cout };
char const delim{ '\"' };
std::vector<std::string> test_strings
{
"24d5n4r05f704n652z393"
, "pi = 3.1415926535897932384626433..."
, "A"
, ""
, "1"
, "123"
, "1234"
};
log << "REQUIRE EXACT LENGTH:\n\n";
for (auto const& s : test_strings)
{
log << "input : " << delim << s << delim
<< "\noutput : " << extract_biggest_number(s)
<< "\n\n";
}
log << "\nALLOW SHORTER LENGTHS:\n\n";
for (auto const& s : test_strings)
{
log << "input : " << delim << s << delim
<< "\noutput : " << extract_biggest_number(s, false)
<< "\n\n";
}
return 0;
}
Here is the output generated by this program:
REQUIRE EXACT LENGTH:
input : "24d5n4r05f704n652z393"
output : "7693"
input : "pi = 3.1415926535897932384626433..."
output : "9998"
input : "A"
output : ERROR: input string does not have any digits
input : ""
output : ERROR: input string does not have any digits
input : "1"
output : ERROR: input string does not have enough digits
input : "123"
output : ERROR: input string does not have enough digits
input : "1234"
output : "1234"
ALLOW SHORTER LENGTHS:
input : "24d5n4r05f704n652z393"
output : "7693"
input : "pi = 3.1415926535897932384626433..."
output : "9998"
input : "A"
output : ERROR: input string does not have any digits
input : ""
output : ERROR: input string does not have any digits
input : "1"
output : "1"
input : "123"
output : "123"
input : "1234"
output : "1234"