Skip to main content
Input must be positive, so it can't remove characters
Source Link
Toby Speight
  • 88.7k
  • 14
  • 104
  • 327

This still leaves a lot of copying when the increment adds a digit (9, 99, 999, ...) or removes a digit (-1, -10, -100, ...) - I think your test-case should include at least one of each. To avoid that problem (and to make the usage more intuitive to the caller), it may be better to write a function that returns a copy of the string (so accept it by const reference):

This still leaves a lot of copying when the increment adds a digit (9, 99, 999, ...) or removes a digit (-1, -10, -100, ...) - I think your test-case should include at least one of each. To avoid that problem (and to make the usage more intuitive to the caller), it may be better to write a function that returns a copy of the string (so accept it by const reference):

This still leaves a lot of copying when the increment adds a digit (9, 99, 999, ...) - I think your test-case should include at least one of each. To avoid that problem (and to make the usage more intuitive to the caller), it may be better to write a function that returns a copy of the string (so accept it by const reference):

added 3 characters in body
Source Link
Toby Speight
  • 88.7k
  • 14
  • 104
  • 327
#include <iostream>
int main()
{
    const std::string str = "$1 $-22 $027 $$ $";
    std::cout << "Result : " << modifyDollarNumber(str) << '\n';
}
#include <iostream>
int main()
{
    const std::string str = "$1 $-22 $027 $";
    std::cout << "Result : " << modifyDollarNumber(str) << '\n';
}
#include <iostream>
int main()
{
    const std::string str = "$1 $-22 $027 $$ $";
    std::cout << "Result : " << modifyDollarNumber(str) << '\n';
}
Add worked code
Source Link
Toby Speight
  • 88.7k
  • 14
  • 104
  • 327

You've misspelt std::size_t throughout, and also std::isdigit (which is missing the necessary include of <cctype> - note also that passing plain char to the character classification functions is risky - cast to unsigned char first).

This still leaves a lot of copying when the increment adds a digit (9, 99, 999, ...) or removes a digit (-1, -10, -100, ...) - I think your test-case should include at least one of each. To avoid that problem (and to make the usage more intuitive to the caller), it may be better to write a function that returns a copy of the string (so accept it by const reference):

#include <algorithm>
#include <cctype>
#include <string>

std::string modifyDollarNumber(const std::string& str)
{
    std::string result;
    result.reserve(str.length());
    auto out = std::back_inserter(result);

    auto pos = str.cbegin();
    while (pos != str.cend()) {
        auto dollar_pos = std::find(pos, str.cend(), '$');
        std::copy(pos, dollar_pos, out);
        // no more substitutions?
        if (dollar_pos == str.cend()) { break; }

        // copy the dollar sign
        result += '$';
        pos = dollar_pos + 1;

        // is it followed by a number?
        auto digit_end = std::find_if(pos, str.end(),
                           [](unsigned char c){ return !std::isdigit(c); });
        if (digit_end == pos) { continue; }

        // copy the incremented number
        auto num = std::stoul(std::string{pos, digit_end});
        result.append(std::to_string(num+1));
        pos = digit_end;
    }

    return result;
}
#include <iostream>
int main()
{
    const std::string str = "$1 $-22 $027 $";
    std::cout << "Result : " << modifyDollarNumber(str) << '\n';
}

But if raw speed is more important than readability, you'll need to benchmark with some representative inputs to see which is best for you.

You've misspelt std::size_t throughout, and also std::isdigit (which is missing the necessary include of <cctype>.

This still leaves a lot of copying when the increment adds a digit (9, 99, 999, ...) or removes a digit (-1, -10, -100, ...) - I think your test-case should include at least one of each. To avoid that problem (and to make the usage more intuitive to the caller), it may be better to write a function that returns a copy of the string (so accept it by const reference).

You've misspelt std::size_t throughout, and also std::isdigit (which is missing the necessary include of <cctype> - note also that passing plain char to the character classification functions is risky - cast to unsigned char first).

This still leaves a lot of copying when the increment adds a digit (9, 99, 999, ...) or removes a digit (-1, -10, -100, ...) - I think your test-case should include at least one of each. To avoid that problem (and to make the usage more intuitive to the caller), it may be better to write a function that returns a copy of the string (so accept it by const reference):

#include <algorithm>
#include <cctype>
#include <string>

std::string modifyDollarNumber(const std::string& str)
{
    std::string result;
    result.reserve(str.length());
    auto out = std::back_inserter(result);

    auto pos = str.cbegin();
    while (pos != str.cend()) {
        auto dollar_pos = std::find(pos, str.cend(), '$');
        std::copy(pos, dollar_pos, out);
        // no more substitutions?
        if (dollar_pos == str.cend()) { break; }

        // copy the dollar sign
        result += '$';
        pos = dollar_pos + 1;

        // is it followed by a number?
        auto digit_end = std::find_if(pos, str.end(),
                           [](unsigned char c){ return !std::isdigit(c); });
        if (digit_end == pos) { continue; }

        // copy the incremented number
        auto num = std::stoul(std::string{pos, digit_end});
        result.append(std::to_string(num+1));
        pos = digit_end;
    }

    return result;
}
#include <iostream>
int main()
{
    const std::string str = "$1 $-22 $027 $";
    std::cout << "Result : " << modifyDollarNumber(str) << '\n';
}

But if raw speed is more important than readability, you'll need to benchmark with some representative inputs to see which is best for you.

Source Link
Toby Speight
  • 88.7k
  • 14
  • 104
  • 327
Loading