Thread: how to write a function to tokenize a string

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    1

    how to write a function to tokenize a string

    I am writing a program which takes two sets of inputs from stdin. Then i need to break up each of these sets into strings using a tokenizer. I want to use a function to do that, so that my program can reuse that function. This is what I have so far.
    Code:
    #include<string.h>
    #include<stdio.h>
    
    int printRecord(char *line)
    {
    char *v_array[50];
    int loop;
    char v_line[1024];
    strcpy(v_line,line);
    v_array[0]=strtok(v_line," ");
    printf("zeroth element is %s\n",v_array[0]);
    if(v_array[0]==NULL)
    {
    printf("No test to search.\n");
    return 0;
    }
    
    for(loop=1;loop<50;loop++)
    {
    v_array[loop]=strtok(NULL," ");
    printf("element is %s\n",v_array[loop]);
    if(v_array[loop]==NULL)
    break;
    }
    
    for(loop=0;loop<50;loop++)
    {
    if(v_array[loop]==NULL)
    break;
    printf("Item #%d is %s.\n",loop,v_array[loop]);
    }
    
    return 0;
    }
    int main(void)
    {
        int i;
    
    char search_string[]="Woody Norm Cliff";
    printRecord(search_string);
    
    return 0;
    }
    Now I want to use the function printRecord in a way that it returns me the set of items tokenized. Either in an array or any other way. How to do that in C?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Y'know, unindented code is a pretty big giveaway that you're just another drive-by copy/paste programmer.

    This looks pretty similar to this
    strtok in C description

    Sure it would be easy to fix the code to do what you want, but the chances of you understanding any of it would be slim. Which would beg more questions or you would just run off to teacher with an answer, or "pretend" that the development done here was your own work on some other forum.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by Salem View Post
    Y'know, unindented code is a pretty big giveaway that you're just another drive-by copy/paste programmer.

    This looks pretty similar to this
    strtok in C description
    Lol...he couldn't even be bothered to change the search string. That is too much.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. tokenize a string
    By Whilst in forum C Programming
    Replies: 6
    Last Post: 10-20-2009, 09:00 PM
  2. tokenize method
    By e4321 in forum C++ Programming
    Replies: 3
    Last Post: 01-24-2009, 12:07 AM
  3. Tokenize string from text file
    By young-ie in forum C Programming
    Replies: 6
    Last Post: 04-16-2007, 09:07 AM
  4. Function to write string 3 times
    By Giggs in forum C++ Programming
    Replies: 15
    Last Post: 12-24-2002, 04:00 PM
  5. How to write a function that returns a string?
    By Yin in forum C++ Programming
    Replies: 8
    Last Post: 03-13-2002, 02:56 PM

Tags for this Thread