2

I have a string..."APPLES" and i'm having difficulty using substring to effectively manipulate the string. My goal is to add a '-' every 3 characters. My problem is that when "APPLES" goes through, it returns "APP-ES-" which is incorrect, I'm trying to make it return "APP-LES-" any suggestions? Here is my code thus far...

 for(int j = 0; j <= str.length(); j++){
    str_substr += str.substr(j,j+3);
    str_substr = str_substr + '-';
    j = j+3;
    cout << str_substr;
}
2
  • your j++ will do that, change it to j += 3 and remove the j = j+3 from the body Commented Oct 26, 2013 at 21:41
  • Having both j++ and j = j + 3 is horrible. For your fix, don't run j up to the end of the string Commented Oct 26, 2013 at 21:41

3 Answers 3

5

Just build a separate string by copying the relevant parts.

std::string s;
for(size_t i = 0; i < str.length(); i += 3) {
    s += str.substr(i, 3) + "-";
}

(Just so you note it for sure: str.substr(j,j+3); is incorrect, it won't copy 3 characters, it will copy j + 3 characters. Read the documentation more carefully.)

Sign up to request clarification or add additional context in comments.

2 Comments

+1 (especially for the size_t) but you ought to mention the pitfalls of repeated string concatenation.
@Bathsheba Thanks! Well, maybe yes :) I can't be bothered to care about performance until the program is correct, though ;-) (and it will be optimized out anyway.)
1

You're incrementing j twice:

for(int j = 0; j <= str.length(); j++){
                                  ^-- once here
...
    j = j+3;
    ^-- and again here

Also, it looks like you might get two -s at the end of a string with a length that's a multiple of three, since you're checking for j <= str.length() instead of j < str.length() Try:

for(size_t j = 0; j < str.length(); j+=3){
    str_substr += str.substr(j,3) + '-';
}
cout << str_substr;

Comments

0

Since you're apparently just copying the result to cout, anyway, perhaps it's easiest to skip the intermediate string, and just copy 3 characters to cout, then write a -, three more characters, etc.

#include <string>
#include <iterator>
#include <iostream>

int main(){
    std::string input("APPLES");

    size_t len = 3;

    for (size_t pos = 0; pos < input.length(); pos += len)
        std::cout << input.substr(pos, len) << "-";     
}

If you need to modify the existing string, you probably want to start by computing the number of dashes you're going to insert, then work from the end of the string back to the beginning, moving each character directly to its destination. If you start at the front and insert dashes where needed, it ends up as an O(N2) algorithm. Working from end to beginning and moving each character directly to its destination is O(N) instead.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.