Seperating the contents of a string into individual components.

Seperating the contents of a string into individual components.

Post by Chri » Thu, 08 Nov 2001 02:35:17



Hi,
I'm having trouble taken a string and splitting it by white space into its
own components.

Ex: "my name is joe blow"
var = my
var2 = name
var3 = is
var4 = joe
var5 = blow

Heres what I have to date:
***********************************
Where command_entry is the original string and command_entry_sep is the new
array of elements of that string.

 /* Seperate the words from the input string */
   for (x = 0; x < strlen(command_entry); x ++) {
          if (strcmp(command_entry[x], " ") == 0){ /* Found a space */
             while (y < x) {
                  command_entry_sep[total_commands][y] =
command_entry[x][y];
             }
             total_commands += 1;
             y = x + 1; /* May have to add one here to avoid space */
      }
   }
It keeps core dumping for some reason...any help would be great!
Cheers,
Chris

 
 
 

Seperating the contents of a string into individual components.

Post by Morris Dove » Thu, 08 Nov 2001 02:47:06



> I'm having trouble taken a string and splitting it by white space into its
> own components.

> Ex: "my name is joe blow"
> var = my
> var2 = name
> var3 = is
> var4 = joe
> var5 = blow

> Here's what I have to date:
> ***********************************
> Where command_entry is the original string and command_entry_sep is the new
> array of elements of that string.

>  /* Seperate the words from the input string */
>    for (x = 0; x < strlen(command_entry); x ++) {
>           if (strcmp(command_entry[x], " ") == 0){ /* Found a space */
>              while (y < x) {
>                   command_entry_sep[total_commands][y] =
> command_entry[x][y];
>              }
>              total_commands += 1;
>              y = x + 1; /* May have to add one here to avoid space */
>       }
>    }
> It keeps core dumping for some reason...any help would be great!

Chris...

The variables total_commands and y aren't initialized.

You might find http://www.iedu.com/mrd/c/tokenize.c interesting.
--
Morris Dovey
West Des Moines, Iowa USA

 
 
 

Seperating the contents of a string into individual components.

Post by Chri » Thu, 08 Nov 2001 02:56:59




> > I'm having trouble taken a string and splitting it by white space into
its
> > own components.

> > Ex: "my name is joe blow"
> > var = my
> > var2 = name
> > var3 = is
> > var4 = joe
> > var5 = blow

> > Here's what I have to date:
> > ***********************************
> > Where command_entry is the original string and command_entry_sep is the
new
> > array of elements of that string.

> >  /* Seperate the words from the input string */
> >    for (x = 0; x < strlen(command_entry); x ++) {
> >           if (strcmp(command_entry[x], " ") == 0){ /* Found a space */
> >              while (y < x) {
> >                   command_entry_sep[total_commands][y] =
> > command_entry[x][y];
> >              }
> >              total_commands += 1;
> >              y = x + 1; /* May have to add one here to avoid space */
> >       }
> >    }
> > It keeps core dumping for some reason...any help would be great!

> Chris...

> The variables total_commands and y aren't initialized.

> You might find http://www.iedu.com/mrd/c/tokenize.c interesting.
> --
> Morris Dovey
> West Des Moines, Iowa USA

I actually did initialyze them to 0....put I missed that bit of code when I
was copying and pasting in the msg. So they are
initialyzed. I'm checking out the code you refered me to.
Thanks,
Chris
 
 
 

Seperating the contents of a string into individual components.

Post by Chri » Thu, 08 Nov 2001 03:25:21






> > > I'm having trouble taken a string and splitting it by white space into
> its
> > > own components.

> > > Ex: "my name is joe blow"
> > > var = my
> > > var2 = name
> > > var3 = is
> > > var4 = joe
> > > var5 = blow

> > > Here's what I have to date:
> > > ***********************************
> > > Where command_entry is the original string and command_entry_sep is
the
> new
> > > array of elements of that string.

> > >  /* Seperate the words from the input string */
> > >    for (x = 0; x < strlen(command_entry); x ++) {
> > >           if (strcmp(command_entry[x], " ") == 0){ /* Found a space */
> > >              while (y < x) {
> > >                   command_entry_sep[total_commands][y] =
> > > command_entry[x][y];
> > >              }
> > >              total_commands += 1;
> > >              y = x + 1; /* May have to add one here to avoid space */
> > >       }
> > >    }
> > > It keeps core dumping for some reason...any help would be great!

> > Chris...

> > The variables total_commands and y aren't initialized.

> > You might find http://www.iedu.com/mrd/c/tokenize.c interesting.
> > --
> > Morris Dovey
> > West Des Moines, Iowa USA

> I actually did initialyze them to 0....put I missed that bit of code when
I
> was copying and pasting in the msg. So they are
> initialyzed. I'm checking out the code you refered me to.
> Thanks,
> Chris

Ok,
I checked out the code you refered me to. I'm looking for something a little
simpler along the lines of what I was writing.
If what I was writing was even possible. *sigh*
Thanks anyways though.
Chris
 
 
 

Seperating the contents of a string into individual components.

Post by Morris Dove » Thu, 08 Nov 2001 03:56:01



> I actually did initialyze them to 0....put I missed that bit of code when I
> was copying and pasting in the msg. So they are
> initialyzed. I'm checking out the code you refered me to.

for (x = 0; x < strlen(command_entry); x ++)
{  if (command_entry[x] == " ")  /* Found a space */
   {  while (y < x)
      {  command_entry_sep[total_commands][y] = command_entry[y];
         ++y;
      }
      total_commands += 1;
      y = x + 1; /* May have to add one here to avoid space */
   }

Quote:}

Chris...

I've made some small changes: [1]the subscripting of command_entry[], and [2]
incrementing y after moving each character into command_entry_sep[][].

If you're still having problems, take a close look at the definition of
command_entry_sep[][].

What happens if tokens are separated by multiple spaces?
--
Morris Dovey
West Des Moines, Iowa USA

 
 
 

Seperating the contents of a string into individual components.

Post by Morris Dove » Thu, 08 Nov 2001 03:58:02



> {  if (command_entry[x] == " ")  /* Found a space */

Very ugly! The " " should have been ' '. Sorry.
--
Morris Dovey
West Des Moines, Iowa USA
 
 
 

Seperating the contents of a string into individual components.

Post by Chri » Thu, 08 Nov 2001 04:07:28




> > {  if (command_entry[x] == " ")  /* Found a space */

> Very ugly! The " " should have been ' '. Sorry.
> --
> Morris Dovey
> West Des Moines, Iowa USA

THANKS alot...I'm trying it out now. That looks much better. I actually
caught the ++y after I posted. The other changes look right...boy do I feel
stupid.
Cheers,
Chris
 
 
 

Seperating the contents of a string into individual components.

Post by Chri » Thu, 08 Nov 2001 04:34:08


Hi,
Well your code works....however I think I have a big problem with my gets.
I used gets instead of scanf because scanf could not get all the words
inputed.
Now for some reason I cant individualy print out the chars from the returned
string from gets.
ex:

   char *command_entry[100];
   char *command_entry_sep[100];

   gets(command_entry); /* used gets to fix scanf problem */
   printf ("\n %s", command_entry); <-- THIS at least works
   printf ("\n 1: %c", command_entry[1]); <-- NOTHING prints here no mater
what is inputed.
   printf ("\n 3: %c", command_entry[3]); <-- NOTHING
  printf ("\n 5: %c", command_entry[5]); <--- nothing.

   /* Seperate the words from the input string */
   for (x = 0; x < strlen(command_entry); x ++){
    printf ("\nINSIDE THE FOR LOOP!");
      printf ("\nThis is the letter: %c\n",command_entry[x]);
     if (command_entry[x] == " "){ /* Found a space */
        while (y < x){
          command_entry_sep[total_commands][y] = *command_entry[y];
          ++y;
       }
         total_commands += 1;
       y = x + 1; /* May have to add one here to avoid space */
    }
 }

 
 
 

Seperating the contents of a string into individual components.

Post by Morris Dove » Thu, 08 Nov 2001 05:31:34



> Now for some reason I cant individualy print out the chars from the returned
> string from gets.
>    char *command_entry[100];

Whoa! Let's make that "char command_entry[100];" instead. You defined an array
of 100 pointers to char - all we need is an array of 100 char. Ok?

Quote:>    char *command_entry_sep[100];

This doesn't agree with usage below where it's used as a two dimensional array.

Quote:>    gets(command_entry); /* used gets to fix scanf problem */

This is a stopgap fix (please) - gets() is evil and dangerous anywhere - fgets()
is very much better.

Quote:>    printf ("\n %s", command_entry); <-- THIS at least works
>    printf ("\n 1: %c", command_entry[1]); <-- NOTHING prints here no matter
> what is inputed.

Well, what can one expect? Command entry[1] is a pointer (to the compiler.)

Quote:>   printf ("\n 3: %c", command_entry[3]); <-- NOTHING
>   printf ("\n 5: %c", command_entry[5]); <--- nothing.

Same problems  8^|

Quote:>    /* Seperate the words from the input string */
>    for (x = 0; x < strlen(command_entry); x ++){
>     printf ("\nINSIDE THE FOR LOOP!");
>       printf ("\nThis is the letter: %c\n",command_entry[x]);
>      if (command_entry[x] == " "){ /* Found a space */
>         while (y < x){
>           command_entry_sep[total_commands][y] = *command_entry[y];
>           ++y;
>        }
>          total_commands += 1;
>        y = x + 1; /* May have to add one here to avoid space */
>     }
>  }

Hang in there!
--
Morris Dovey
West Des Moines, Iowa USA
 
 
 

Seperating the contents of a string into individual components.

Post by Chri » Thu, 08 Nov 2001 05:48:15




> > Now for some reason I cant individualy print out the chars from the
returned
> > string from gets.

> >    char *command_entry[100];

> Whoa! Let's make that "char command_entry[100];" instead. You defined an
array
> of 100 pointers to char - all we need is an array of 100 char. Ok?

> >    char *command_entry_sep[100];

> This doesn't agree with usage below where it's used as a two dimensional
array.

> >    gets(command_entry); /* used gets to fix scanf problem */

> This is a stopgap fix (please) - gets() is evil and dangerous anywhere -
fgets()
> is very much better.

> >    printf ("\n %s", command_entry); <-- THIS at least works
> >    printf ("\n 1: %c", command_entry[1]); <-- NOTHING prints here no
matter
> > what is inputed.

> Well, what can one expect? Command entry[1] is a pointer (to the
compiler.)

> >   printf ("\n 3: %c", command_entry[3]); <-- NOTHING
> >   printf ("\n 5: %c", command_entry[5]); <--- nothing.

> Same problems  8^|

> >    /* Seperate the words from the input string */
> >    for (x = 0; x < strlen(command_entry); x ++){
> >     printf ("\nINSIDE THE FOR LOOP!");
> >       printf ("\nThis is the letter: %c\n",command_entry[x]);
> >      if (command_entry[x] == " "){ /* Found a space */
> >         while (y < x){
> >           command_entry_sep[total_commands][y] = *command_entry[y];
> >           ++y;
> >        }
> >          total_commands += 1;
> >        y = x + 1; /* May have to add one here to avoid space */
> >     }
> >  }

> Hang in there!
> --
> Morris Dovey
> West Des Moines, Iowa USA

Got it working....now I've got a barrel of new problems. *grin*
Thanks alot for your help!
Cheers,
Chris
 
 
 

Seperating the contents of a string into individual components.

Post by Eric Sosma » Thu, 08 Nov 2001 05:39:58



> Hi,
> Well your code works....however I think I have a big problem with my gets.
> I used gets instead of scanf because scanf could not get all the words
> inputed.

    *Any* use of gets() is a big problem; see Question 12.23 in
the comp.lang.c Frequently Asked Questions (FAQ) list

        http://www.eskimo.com/~scs/C-faq/top.html

to learn why you should always[*] use fgets() instead of gets().

    [*] Some of the pedants in comp.lang.c maintain that it is
        possible to use gets() safely, if the circumstances are
        rigidly controlled.  I am also a pedant, but I feel that
        (1) it's surpassingly hard to control the circumstances
        rigidly enough and (2) it's so easy to use fgets() that
        there's no point in even attempting such rigid control.

    Don't use gets().  Don't.  Period.

Quote:> Now for some reason I cant individualy print out the chars from the returned
> string from gets.
> ex:

>    char *command_entry[100];
>    char *command_entry_sep[100];

>    gets(command_entry); /* used gets to fix scanf problem */

    Quite aside from the use of gets(), this is just plain
wrong.  gets() and fgets() both work with arrays of `char', but
`command_entry' is not such a beast: it is an array of pointers
to `char', not an array of `char'.  You should have omitted the
asterisk from its declaration and written

        char command_entry[100];

Quote:>    printf ("\n %s", command_entry); <-- THIS at least works

    If it works, it's by pure dumb luck.

Quote:>    printf ("\n 1: %c", command_entry[1]); <-- NOTHING prints here no mater
> what is inputed.

    I am not surprised.  The argument corresponding to a "%c"
conversion must be an `int' whose value is a character code.
`command_entry[1]' is not an `int', but a pointer to `char'.

Quote:>    printf ("\n 3: %c", command_entry[3]); <-- NOTHING
>   printf ("\n 5: %c", command_entry[5]); <--- nothing.

>    /* Seperate the words from the input string */
>    for (x = 0; x < strlen(command_entry); x ++){
>     printf ("\nINSIDE THE FOR LOOP!");
>       printf ("\nThis is the letter: %c\n",command_entry[x]);
>      if (command_entry[x] == " "){ /* Found a space */

    Surround character constants with single quotes ' rather
than with double quotes ".

Quote:>         while (y < x){

    Had `y' been assigned any particular value before reaching
this statement for the first time?

Quote:>           command_entry_sep[total_commands][y] = *command_entry[y];

    This is clearly wrong, but it's so wrong I can't discern
your intention and suggest how to fix it.  What are you trying
to accomplish?

Quote:>           ++y;
>        }
>          total_commands += 1;
>        y = x + 1; /* May have to add one here to avoid space */
>     }

 }

--

 
 
 

Seperating the contents of a string into individual components.

Post by Donald McLachl » Fri, 09 Nov 2001 01:19:57



> Hi,
> I'm having trouble taken a string and splitting it by white space into its
> own components.

> Ex: "my name is joe blow"
> var = my
> var2 = name
> var3 = is
> var4 = joe
> var5 = blow

I've sort of been following this thread.  I know you've said some answers are
more complex than you want, so you might not like this one, but here is how
I would code what you want.

Sample output:

Quote:> a.out

parser(::, 0) =
parser(:Hi:, 0) = :Hi:
parser(:How now brown cow?:, 0) = :How: :now: :brown: :cow?:

source code: [No error checking, but written so (hopefully) easy to understand]

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

char **parse(char *string, int word)    /* caller frees retval[0] and retval */
{
        char **retval;
        char *sow;
        int index;

        sow = string;                                   /* start of word */
        index = word;                                   /* where to store sow */
        if(*string == '\0')                             /* end of string */
        {
                retval = malloc(sizeof(char *) * word+1);
                retval[word] = NULL;
                return(retval);
        }

        while((*string != '\0') && !isspace((int)*string))
                ++string;               /* find end of string or end of word */

        if(isspace((int)*string))                       /* end of word */
        {
                *string = '\0';                         /* null term word */
                ++string;                               /* parse next word */
        }

        retval = parse(string, word+1);         /* parse next word */
        retval[word] = sow;                     /* save word */
        return(retval);

Quote:}

char **parser(char *string)     /* wrapper for parse() */
{
        char *copy;

        copy = strdup(string);          /* copies string to preserve orig */
        return parse(copy, 0);          /* hides second arg from user */

Quote:}

int main(int argc, char *argv[])
{
        char *strings[] = {"", "Hi", "How now brown cow?"};
        char **words;
        int i, j, n;

        n = sizeof(strings)/sizeof(strings[0]);
        for(i = 0; i < n; ++i)
        {
                words = parser(strings[i]);
                printf("parser(:%s:, 0) = ", strings[i]);
                for(j = 0; words[j] != NULL; ++j)
                        printf(":%s: ", words[j]);
                printf("\n");
                free(words[0]);                         /* copy of string */
                free(words);                            /* results */
        }

        return(0);

Quote:}

--

Communications Research Centre / RNS    Tel     (613) 998-2845
3701 Carling Ave.,                      Fax     (613) 998-9648
Ottawa, Ontario
K2H 8S2
Canada
 
 
 

Seperating the contents of a string into individual components.

Post by Chri » Fri, 09 Nov 2001 10:48:35


Thank you all for your help...I've actually managed to figure it out....but
I used a different approach.
Granted this approach may be wrong but its seperating everything
accordingly...
I will look into using fgets instead of gets. The point has been made that I
shouldnt use it.
Tell me what you think.
Cheers,
Chris

Code
------------

   char *p;
   char *delim = " ";
   gets(command_entry); /* will most likely be changing this... */
   p = strtok(command_entry,delim);

   while ( p ) {
     strcpy( command_entry_sep[total_commands], p );
     p = strtok( NULL, delim );
     total_commands++;
   }

This does work. It seperates it into seperate variables just fine. But I was
wondering if there are problems using this
approach. I have yet to see them. But I use code later on, that uses the
seperate vars to compare against other strings.
As far as I know its all good up to now...

 
 
 

Seperating the contents of a string into individual components.

Post by S. Kru » Fri, 16 Nov 2001 06:22:51



> Hi,
> I'm having trouble taken a string and splitting it by white space into its
> own components.

> Ex: "my name is joe blow"
> var = my
> var2 = name
> var3 = is
> var4 = joe
> var5 = blow

> Heres what I have to date:
> ***********************************
> Where command_entry is the original string and command_entry_sep is the new
> array of elements of that string.

>  /* Seperate the words from the input string */
>    for (x = 0; x < strlen(command_entry); x ++) {
>           if (strcmp(command_entry[x], " ") == 0){ /* Found a space */
>              while (y < x) {
>                   command_entry_sep[total_commands][y] =
> command_entry[x][y];
>              }
>              total_commands += 1;
>              y = x + 1; /* May have to add one here to avoid space */
>       }
>    }
> It keeps core dumping for some reason...any help would be great!
> Cheers,
> Chris

Hello Chris,

I've taken a look at your problem, and have come up with the folowing:

/* Seperate the words from the input string */

#include <ctype.h>
#include <strings.h>

#define MAX_COMMANDS  10

typedef struct _CMD_ENTRY {

  char entry[32];

Quote:} CMD_ENTRY;

int main(int argc, char* argv[], char* envp[])
{
  char command_entry[128];
  CMD_ENTRY command_entry_sep[MAX_COMMANDS];
  int total_commands;
  int x;
  int y;

  total_commands = 0;
  x = 0;
  y = 0;

  memset( command_entry_sep, 0, sizeof( command_entry_sep ) );

  strcpy( command_entry, "my name is joe blow" );

  for ( x = 0; x < strlen( command_entry ); x ++ )
    {
      if ( !isblank( command_entry[x] ) ) /* Found a space */
        {
          if( total_commands < MAX_COMMANDS )    /* Out of bounds? */
            {
              command_entry_sep[total_commands].entry[y] = command_entry[x];

              y++;
            }
         /* else { handle error } */
        }
      else
        {
          y = 0;
          total_commands++;
        }
    }

  for( x = 0; x < total_commands; x++ )
    {
      printf( "Entry %d: %s\n", x, command_entry_sep[x].entry );
    }

  return 0;

Quote:}

Output:
Entry 0: my
Entry 1: name
Entry 2: is
Entry 3: joe

Hope it helps.

Bas
--
Rigtige m?nd bruger ikke BIOS - rigtige m?nd bruger Linux...

 
 
 

Seperating the contents of a string into individual components.

Post by Sean Burk » Mon, 03 Dec 2001 10:01:37


Cheez, man strtok() dudes.
-SEan

 
 
 

1. Content of file is one line has to be displayed in seperated lines

Hello,

a small question i'm not that known with the underwater marks of unix how
things are displayed. Maybe someone has a perfect link to a manual to learn
about it ?

And now the question:

I have a script who's generating some reports, tjust a fwe lines what he's
doing and put it in a file. Next when the script is almost done i put that
information of that file in a varaiabele called "HELP=`cat /tmp/backup.log`"

The HELP varaiable is exported to a homepage and here is the problem, the
report is diplayed in one line. Is it possible to display this tjust like it
was displayed in the /tmp/backup.log file ?

Any help would be great.

Regards,

Jeroen

2. When is an OS multiuser?

3. Seperate contents to 2 Apaches

4. CDROM not listing files

5. how to remove redunt word from space seperated word string

6. CDE Screenlock NOT unlocking...

7. seperate login schemes for seperate users

8. Newbie Questions

9. USL C++ Standard Components "Graph" Component Question

10. How to have 2 component NIS+ domain emulate one component NIS domain?

11. Checking String Contents

12. move content of a string in variables

13. Help about the diff btw MIME's Content-ID and Content-Location header