Separate string as delimiter strtok (C++)

I have a text I need to find in each proposal max word and min word.(Separated by dots) I wrote a function to find it. I'm trying to send first separated proposal to a function

calculateString(token);

. After this variable token is truncated. Thanks for answers!

Updated: Question:I need to found in each proposal max and min text.On the first step it's truncated.How can I avoid it?(Do not truncate after using it) –

Code:

//Output/input /cin/cout
#include <iostream>
//System libary
#include <cstdlib>

//include printf/sprintf/scanf
#include <stdio.h>

//include string libary
#include <string>

//
#include <ctype.h>
#include <conio.h>

using namespace std;

void calculateString(char *str){
    char *temp;
    int len;
    int i = 0;
    char** stringArray;
    string long_word,short_word;

    temp = strtok(str," ,.!?");
    cout << "Str:" <<  str << "\n";
    int string_count = 0;//counting first word in string. 
    int string_count_short = 0;
      stringArray = new char*[string_count];
        while(temp = strtok(NULL," ,.!?"))
        {
                //cout << "\n" << temp << "\n";
                len = strlen(temp);
                if(len>string_count || string_count == 0){
                    string_count = len;
                    long_word =  temp;
                }
                if(len<string_count_short || string_count_short == 0){
                    string_count_short = len;
                    short_word=  temp; 

                }
        }       
       cout << "Longest word in all text is:" <<  long_word << "\n";
       cout << "Shortest word in all text is:" <<  short_word << "\n";
}
int main()
{
   char str[] = "Hello it's a test.Lets go.How are you?Andrej";
   char seps[]   = ".";

   char *token;
   token = strtok( str, seps );
   while( token != NULL )
   {
      /* While there are tokens in "string" */
      cout << " :: " << token << " ||\n";
      calculateString(token);

      /* Get next token: */
      token = strtok( NULL, seps );
   }
    //calculateString(str);

    system("pause");
    return 0;
}

Answers


Try something like this (this is hack)

/*Const string count */
   const int string_count = 10;
   /*Total string found */
   int str_count = 0;
   /* String delimiter*/
   char seps[]   = ".";

   char *token;
   token = strtok( str, seps );

   /* Creating array of chars */
   char** stringArray;
   /* Dynamic array of chars*/
   stringArray = new char*[string_count];

   while( token != NULL )
   {
      /* While there are tokens in "string" */
      stringArray[++str_count] = token; 
      /* Get next token: */
      token = strtok( NULL, seps );
   }
   /* Seperated propsal */
   for(int i=1;i <=str_count;i++){
         calculateString(stringArray[i]);
   }

You're trying to use strtok() in a reentrant fashion, which won't work because strtok() keeps a static state. The calls to strtok() within calculateString() clobber the state from the calls in main().

You might be interested in strtok_r(). Or better yet, look into something like Boost.Tokenizer.


If performance is not extremely sensitive in your app, try STL or Boost library, which will free you from the old-fashion issues.

Hope it helps.


Need Your Help

Subtract months from a given date

matlab date bsxfun date-math

I need to subtract months from a given DateCol in a matrix. I was using the command bsxfun(@minus, datevec(date),[0,3,0,0,0,0]) to subtract 3 months from the given date. You can see there is a b...

Passing variables to Class from HTML

html flash actionscript-3 class variables

I want to pass some image names and numbers as string to my flash, so in html file I have :

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.