tc90
tc90

Reputation: 307

Adding commas to a set of strings stored in an array

I need to add a comma to each string stored in the array according to a position which is stored in the byteSizeBuffer.

std::string::iterator it;
    int position = 0;
    int totalSize = 0;
    for (int i = 0; i < numOfMPs+1; i++)
    {
        for (int j = 0; j < numOfMPs; j++)
        {
            if(totalSize < subSize)
            {
                switch(byteSizeBuffer[j]) {
                    case 2:
                        position = totalSize+4; //4 because in the string each byte is 2 characters (2*2=4)
                        totalSize = position;
                        it = sub_[i].begin()+position, ','; //inserts comma at iterator+position
                        break;
                    case 4:
                        position = totalSize+8;
                        totalSize=position;
                        it = sub_[i].begin()+position, ',';
                        break;
                    default:
                        cout << "An error has occured while splitting the Data";
                        break;
                }
            }
        }
    }
  1. The position is the position which i want the comma to be inserted.
  2. The totalSize is the total of the positions and cannot exceed subSize
  3. subsize is always and even number and always 34 bytes ~68 characters
  4. The commas are added to the same position for every string in the array.

Strings look like: FG454F3423T5245G4G5H6546456Y54645G4456G45G60101000000101010111000001

The positions stored in the bytesizeBuffer[] are stored as either a 2 or a 4 which represent the number of bytes.

So if the numbers stored were 4,4,4,4,2,4,4,2,2,2,2 I need it to look like: FG454F34,23T5245G,4G5H6546,456Y5464,5G44,56G45G60,10100000,0101,0101,1100,0001 ..........No comma should be added at the end of the line.

My above code does not seem to work and i was wondering if i was going about it the correct way or if maybe there was a more efficient way of doing the above.

Just some tips/pointers to get me on the right track is really what i'm looking for. Thanks

Upvotes: 1

Views: 933

Answers (2)

tc90
tc90

Reputation: 307

std::string::iterator it;
    int position = 0;
    int totalSize = 0;
    for (int i = 0; i < numOfMPs+1; i++)
    {
        for (int j = 0; j < numOfMPs; j++)
        {
            /* mp_subSize*2: each byte is 2 chars long so we have to get the full 
             * length of the string according to the characters 
             * +numOfMPs-1: adds the numOfMPs and then subtracts the final size from the buffer and -1 for the comma
             * so that there is no comma added at the end of the string.
            */
            if(totalSize < _subSize*2+numOfMPs-(byteSizeBuffer[count-1]*2)-1)
            {
                switch(byteSizeBuffer[j]) {
                    case 2:
                        position = totalSize+4; //4 because in the string each byte is 2 characters (2*2=4)
                        totalSize = position+1; //number of bytes +1 for the comma
                        break;
                    case 4:
                        position = totalSize+8;
                        totalSize=position+1;
                        break;
                    default:
                        cout << "An error has occured while splitting the Data";
                        break;
                }
                //this line adds the comma
                it = sub_[i].insert(sub_[i].begin()+position, ',');
            }
        }
        totalSize=0;
        position=0;
    }

A more efficient way would be to put this into a function like @Roddy said in the comments. However, this is what i came up with and it works.

Upvotes: 0

Abhishek Bansal
Abhishek Bansal

Reputation: 12715

You can use std::string::insert.
http://en.cppreference.com/w/cpp/string/basic_string/insert

Also, don't forget that after each insertion your string size changes and hence the positions of all proceeding elements.

Upvotes: 1

Related Questions