Thread: Splitting a string into tokens

  1. #1
    unregistered
    Guest

    Unhappy Splitting a string into tokens

    Hi,
    I've been trying to get this to work properly...maybe you guys can help me out here.
    I'm trying to take a string and put each word into an array.
    Ex: This prog is great
    var1 = This
    var2 = prog
    and so on.
    Heres the code I have to date:
    ----------------------
    /* 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; /* May have to add one here to avoid space */
    }
    }
    ----------------------
    both y and total_commands were initialyzed to zero.(earlier in the code) and are still 0.
    This keeps core dumping. *sigh* Any help would be great!
    Thanks in advance,
    Chris

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    247
    Hi Hope this helps
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main ()
    {
    char str[]= "This prog is great", *p;
    char split[4][6];
    
    int i, j;
    p = str;
    for(i = 0; i < strlen(str) - 1; i++)
    {
           j = 0;
          while(*p != ' ' && *p != '\0')
          {
    	split[i][j] = *p;
    	 p++;
    	 j++;
          }
          if(*p == '\0') break;
          p++;
          split[i][j]='\0';
    }
    split[i][j]='\0';
    printf("%s\n", str);
    for(i = 0; i < 4; i++)
         printf("%s ", split[i]);
    }
    This prints out
    This prog is great
    This prog is great
    hoping to be certified (programming in c)
    here's the news - I'm officially certified.

  3. #3
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    seems like a lot of code to do something that there is a function provided to do. That function is strtok(). Look at salems code.The first time it is called it is passed the address of the string to be tokenised. To keep tokenising the same string the next time you call strtok you pass a NULL.This tells strtok() to keep tokenising the string that it has already started. It is possible to change the delimiter as well if necessary between the calls.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    247
    well pointed out stoned coder, although I have never used strtok before, having compiled salems I can well see this would be preferred option, looking up how strtok works at the moment.
    hoping to be certified (programming in c)
    here's the news - I'm officially certified.

  5. #5
    Unregistered
    Guest
    Thanks Salem...You're code worked like a charm!
    Cheers,
    Chris

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String splitting algorithm help
    By (TNT) in forum C++ Programming
    Replies: 7
    Last Post: 05-12-2007, 11:28 AM
  2. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  3. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM
  5. Again Character Count, Word Count and String Search
    By client in forum C Programming
    Replies: 2
    Last Post: 05-09-2002, 11:40 AM