Thread: Custom String Parsing Function

  1. #1
    Unregistered
    Guest

    Custom String Parsing Function

    Hi, I want to make a function Called Gettok(). The idea for this function comes from the mIRC scripting language's function called $gettok. It works like this:

    gettok(text,N,C)
    Returns the Nth 'token' in text.

    Example:
    gettok(this is a string,1,32) returns "this"
    gettok(this is a string,2,32) returns "is"
    gettok(this is a string,3,32) returns "a"
    gettok(this is a string,4,32) returns "string"
    gettok(this is a string,5,32) returns NULL (or "NULL")

    The string "this is a string" is seperated by Character Code 32, which is a space.
    If i wanted too, I could specify a different character code, For example 115, which is the ASCII code for the letter S. And then do this:
    gettok(this is a string,1,115) returns "thi"
    gettok(this is a string,2,115) returns "i"
    gettok(this is a string,3,115) returns "a "
    gettok(this is a string,4,115) returns "tring"
    gettok(this is a string,5,115) returns NULL (or "NULL")

    I would want to use this Function like this:

    String string1 = gettok(Edit1->Text,3,32)

    or

    Char *string[]=gettok(Edit1->Text,2,42)


    Any tips how to make such a function?

  2. #2
    Tips? Sure.

    You could search through the passed string for the occurance of the desired character. Perhaps using 'strchr()' or by cycling through the array and comparing each element.
    Code:
    for (i = 0; i < strlen(PassedInString); i ++)
       if (PassedInString[i] == ThisCharacter) // do something about it
    Then simply get the characters that occured before the matched element and return them. Repeat your search for the required character n times or until you reach the end of the string.

    P.S. This: 'gettok(this is a string,1,32)' will generate any number of errors. You mean this: 'gettok("this is a string",1,32)'
    "There's always another way"
    -lightatdawn (lightatdawn.cprogramming.com)

  3. #3
    Pygmy Monkey ErionD's Avatar
    Join Date
    Feb 2002
    Posts
    408
    Just so you know there already exists such a function, strtok()

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  2. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM