Hi Guys,

I know this will be easy for anyone here and it might take 2 minutes to right the code. I wonder if someone can help me to right a loop using strtok() to be able to extract the words form a string like "various systems analyze the model create a model of a desired version" and store each word into a array.

thanks a lot guys.

What have you tried so far? Why don't you modify the example shown here?

Hi,

The below code is what I'm using but for some reason is repeating the first word of the string twice.

#include <stdio.h>
#include <string.h>
#include <strings.h>
int main (void)
{
int a;
char temp[] = "<title>probably be that of the total potential available </title><title>The compost process combines the natural organic mater
ials</title>" ;
printf ("%s\n", temp) ;
char* string1 = temp ;
int stringLen = 0;
char output[100];
while(*string1 != '\0')
{
  string1 = strpbrk(string1, ">" ) ;
  string1++ ;
  stringLen = strcspn (string1, "<") ;
  if(stringLen >= 1)
  {
    strncpy(output,string1,stringLen);
    output[stringLen] = '\0';
/*    printf ("%s\n", output) ; */
    string1 = string1 + stringLen;
  }
   char delims[] = " ";
   char *result = NULL;
   result = strtok( output, delims );
   while( result != NULL ) {
       printf( " %s \n", result );
       result = strtok( NULL, delims );
}
}
return 0 ;
}

and the output of this code is :

probably
 be
 that
 of
 the
 total
 potential
 available
 probably
 The
 compost
 process
 combines
 the
 natural
 organic
 materials
 The

But you can see "probably" and "The" are repeate it twice. Any ideas. thanks.

#include <stdio.h>
#include <string.h>
int main (void)
{
    int a;
    char temp[] = "<title>probably be that of the total potential available</title><title>The compost process combines the natural organic materials</title>" ;
    printf ("%s\n", temp) ;
    char* string1 = temp ;
    int stringLen = 0;
    char output[100];
    while(*string1 != '\0')
    {
        // Find the first '>'
        string1 = strpbrk(string1, ">" ) ;
        string1++ ;
        // Search for the next '<'
        stringLen = strcspn (string1, "<") ;
        if(stringLen >= 1)
        {
            strncpy(output,string1,stringLen);
            output[stringLen] = '\0';
            string1 = string1 + stringLen;

            char delims[] = " ";
            char *result = NULL;
            result = strtok( output, delims );
            while( result != NULL )
            {
                printf( " %s \n", result );
                result = strtok( NULL, delims );
            }
        }
    }
    return 0 ;
}

Thanks Once again!!!!.

Hi,

One more thing almost forget. How can i check/control that if for any reason the '<' character is not in the same line but maybe a few lines after. The code works fine but when the '<' character is not on the same line all those words are left out. For example:

<img src="mact.gif" align="left" hspace=12 >More precisely, ...
<p>

In your former code, the data string was like this. There were no new lines in it.

char temp[] = "<title>probably be that of the total potential available </title><title>The compost process combines the natural organic materials</title>" ;

What is your new data string?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.