0

Well.. I can't describe this issue any better than current title but if you can, please go ahead. :)

I'm trying to parse 1000 char string which contains digits. And I want to divide this string from '0' character and put remaining adjacent characters in a list. For example;

string data = "12304560987598705775068";

Now, under normal circumstances, my desired aim is to divide it from 0 chars and omit zeros. Like the following numbers and put them in a list: 123 456 9875987 5775 68

Below you can see my code. It works well with current "data" string but i can't make it work the other string in the comment.

Here's my compiler info:

gcc --version Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk/usr/include/c++/4.2.1 Apple LLVM version 8.0.0 (clang-800.0.38) Target: x86_64-apple-darwin16.1.0 Thread model: posix InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

I'm using Netbeans 8.2 on macOS Sierra

And here's my code:

#include <cstdlib>
#include "iostream"

// following are required for list
#include <algorithm>
#include <list>

using namespace std;

//string data = "7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450";
string data = "123045609875987057750687687765554650760078901";

list<string> dS; // dS stands for data set
string particle = ""; // represents each element of dS during runtime

void prepareDataSet(); // gets data set into dS


int main(int argc, char** argv) {

    prepareDataSet();

    return 0;
}

void prepareDataSet(){
    cout << "iteration begins" << endl;
    for(int i=0; i<= sizeof(data); i++){
        if(data.at(i) != '0') {
            particle += data[i];
        }
        else {
            dS.push_back(particle);
            cout << "particle: " << particle << endl;
            particle = "";

            // following deletes from beginning to first 0(zero)

            data.erase(0,(i+1));
            cout << "remaining: " << data << endl;
            cout << "iteration ends" << endl << "************";
            if(sizeof(data)>0) {
                prepareDataSet();
            }
            else {
                break;
            }
        }
    }
}
t1w
  • 1,408
  • 5
  • 25
  • 55
  • 1
    `sizeof(data)` is not what you expect, you want `data.size()`. – Jarod42 Nov 25 '16 at 11:33
  • And you want to stop to `i < data.size()`, not `i <= data.size()` – rocambille Nov 25 '16 at 11:36
  • You may look at [c-split-string-by-a-character](http://stackoverflow.com/questions/20755140/c-split-string-by-a-character). – Jarod42 Nov 25 '16 at 11:37
  • By the way, your current code is very inefficient for long strings. If you care about this, store the result in a vector and do not modify the input string by erasing from the front of it repeatedly--just iterate over it once without modifying it. – John Zwinck Nov 25 '16 at 12:29

0 Answers0