Question

I have a function that takes two strings and determines if they are the same. I am trying to tokenize the string and combine all of tokens into one string. This is what I have so far and I am getting Bus error :10.
any help appreciated.

    #include <iostream>
    #include <string>
    using  namespace std;

    bool stringCheck(string s1, string s2){
    string strCheck1 = "";
    string strCheck2 = "";

    char *cstr1 = new char[s1.length()]; // char array with length of string
    strcpy(cstr1, s1.c_str());  // copies characters of string to char array

    char *cstr2 = new char[s2.length()];
    strcpy(cstr2, s2.c_str());

    char *p1 = strtok(cstr1, " ");  // creates a char array that stores token that 
                                    // is delimeted 
    cout << "p1 " << p1 << endl;    ///outputs token that is found

    strCheck1.append(p1);                       // appends token to string 
    cout << "strCheck1  " << strCheck1 << endl; // outputs string

    while(p1 != NULL)               // while the token is not a null character
    {
        cout<<"parsing" << endl;    
        p1 = strtok(NULL, " ");     // continue to parse current string.  
        cout << "p1 " << p1 << endl; 
        strCheck1.append(p1);
        cout << "str1  " << strCheck1 << endl;
    }

    char * p2 = strtok(cstr2, " ");
    cout << "p2 " << p2 << endl; 
    strCheck2.append(p2);
    cout << "strCheck2  " << strCheck2 << endl;

    while(p2 != null){
        p2 = strtok(NULL, " ");
        strCheck2.append(p2);
        cout << "str2  " << strCheck2 << endl;
    }

    if( strCheck1.compare(strCheck2) != 0)
    {
        return 0;
    }
    else return 1;
}

int main(void){
    string s1 = "jam yoooo jay";
    string s2 = "jam    yoooo";
    if(stringCheck(s1, s2) == 1){
        cout << "strings same"<< endl;;
    }
    else{
        cout << "strings not same" << endl;
    }

}

is there a conditional statement I could pair up with

while(p1 != NULL)

I know this is a pretty silly function but just trying to polish up my skills. any help appreciated!

Was it helpful?

Solution

There are some things you must change:

  • char *cstr1 = new char[s1.length()];

    c-string are null-terminated, so you need one more char to store the null character:

    char *cstr1 = new char[s1.length() + 1];

    (same for cstr2)

  • strCheck1.append(p1)

    p1 cannot be a null pointer (see Assign a nullptr to a std::string is safe? for further details). So you have to check...

    if (p1) strCheck1.append(p1);

    (same for p2).

  • cout << p1 << endl

    if p1 is a null pointer bad things can happen (see Why does std::cout output disappear completely after NULL is sent to it). So you have to check...

    if (p1) { cout << "p1 " << p1 << endl; strCheck1.append(p1); }

    (same for p2)

  • there is a memory leak (cstr1 / cstr2 must be deleted).

At the end it should work.

Probably you should consider other systems to extract tokens (where you haven't to mix std::string and c-string). E.g.:

#include <iostream>
#include <string>
#include <sstream>

int main()
{
  std::string text("text-to-tokenize");
  std::istringstream iss(text);
  std::string token;

  while(getline(iss, token, '-'))
    std::cout << token << std::endl;

  return 0;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top