Insert a character every N characters in a string in C ++, changing the N character

I am trying to use:

std::string tokenize(const std::string& s) {
   if (!s.size()) {
     return "";
   }
   std::stringstream ss;
   ss << s[0];
   for (int i = 1; i < s.size(); i++) { // tried i++ ----> i+=16
     ss << '|' << s[i];
   }
   return ss.str();
}

      

from ( How to insert a character every N characters in a string in C ++ ), but changing "|"

inserted from every character every 16 characters.

I tried to change i++

----> i+=16

but it fails and "sqdfqdfqwerqwer"

will become"s|q"

Can anyone find my error?

+3


source to share


4 answers


 for (int i = 1; i < s.size(); i+=16) { // tried i++ ----> i+=16

     ss << '|' << s[i];
   }

      

Firstly:

but it fails and "sqdfqdfqwerqwer"

will become"s|q"

You jump from 1st position ( i=1

) to 16th position ( i+=16

) of the given row directly. When the loop is executed a second time, the position is at 1+16

th postion.

so ss

gets a string s[0],s[i],s[i+16],s[2i+16]...

so you get a stripped-down version of string s.

The solution is quite simple, as already mentioned above.



for (int i = 1; i < s.size(); i++) {
     if (i%16==0) ss << '|'; <<-- this is the main concept you are missing
     ss << s[i];
   }

      

  • We iterate over all characters in the string

  • check if we are in the right place. for more information on how the Modulo operator works.

  • if we get the position we want, we will do what you want to do.

Now that I transmit

DoesThisAnswerYourQuestion?ImGladItDid.IfNotITriedMybest:D

deduces

DoesThisAnswerYo|urQuestion?ImGla|dItDid.IfNotITri|edMybest:D

+3


source


for (int i = 1; i < s.size(); i++) {
     if (i%16==0) ss << '|';
     ss << s[i];
   }

      



+1


source


I tried changing i ++ ----> i + = 16 but that doesn't help and makes "sqdfqdfqwerqwer" become "s | q"

The error is that you are incrementing 16. You are putting the first character on the stream. Then you loop once and put the |

next character on the stream as well. Then you increment by 16, which results in a i

larger row size. Thus, the loop is completed and the specified result is printed.

@ kirbyfan64os suggested a fix. Happy coding.

+1


source


Walking around

ss << s[0];

      

The first character goes to the output stream

for (int i = 1; 

      

Starting from 1

     i < s.size(); 

      

and continue until you reach or pass the end of the line

     i++) 

      

Look at each element of the array

    ss << '|' << s[i];

      

put | and the current character to the output stream.

So, for "ABC", the loop collapses into:

ss << A;      //ss << s[0];
ss << | << B; //ss << '|' << s[1];
ss << | << C; //ss << '|' << s[2];

      

Stream content: A | B | C

for (int i = 1; i < s.size(); i+=16)

      

The tool considers the second element and every 16th element after the line. In other words, s [1], s [17], s [33], s [49] ...

So for "sqdfqdfqwerqwer" you expand to:

ss << s;      //ss << s[0];
ss << | << q; //ss << '|' << s[1];

      

And there are only 15 characters in the string, so 17 is not. Output:

s|q 

      

As the OP pointed out.

OK. Screw this last time, so get two. This can be done without a string buffer by concatenating strings, but a string stream is probably the faster choice. I would have to profile to prove it. I also made a quick setup to allow the caller to specify the length. This was useful for testing, so I left it.

std::string tokenize(const std::string& s,
                     size_t where)
{
    if (s.size() > 0)
    {
        std::stringstream temp;
        temp << s.substr(0,where);
        for (size_t loc = where; loc < s.size(); loc+=where)
        {
            temp << '|' << s.substr(loc,where);
        }
        return temp.str();
    }
    return "";
}        

      

Results:

In =  "I'm the very model of a modern major general" 
Out = "I'm the very mod|el of a modern m|ajor general"

      

+1


source







All Articles