Thread: Parsing strings

  1. #16
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Well I would imagine your average C++ developer would not wish to have [some][crazy][multi][dimensional][array][mess][such][as][this][anyway]. But in C people ask all the time....

  2. #17
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Indeed. I think the biggest use is an array of strings which is abstracted away with std::string or string classes in C++.
    I have found little use for multi-dimensional arrays in C++ and when I've had the use of one, it's been for only 2D.
    Of course I haven't worked on games, so my mileage may vary on this topic.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #18
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Well I try not to use multidimensional arrays in games due to the fact they are inefficient. Though most will argue "that is what my optimizer is for" I can only site that an optimizer is like a car or a power drill. Its a tool. Its not perfect. Where I can help it along, I do prefer to do so. People too frequently cut so many corners based on the preface that their compiler will fix everything. I have found in my experience that calculating one array index once and applying it to multiple arrays is much faster than multidimensional arrays.

  4. #19

    Join Date
    Apr 2008
    Location
    USA
    Posts
    76
    This is something I made in C:
    (It works like the PHP explode().)

    Code:
    char** str_explode(separator, str)
        const char separator;
        char* str;
    {
        int len = strlen(str);
        int nSeparators = 0;
    
        int* length = (int*) malloc( sizeof(int) * len );
    
        int i = 0;
        for(; i < len; ++i)
            *(length + i) = 0;
    
        i = 0;
        for(; i < len; ++i)
        {
            if( *(str + i) == separator )
                ++nSeparators;
            else
                *(length + nSeparators) += 1;
        }
    
        int start = 0;
        char** results = (char**) malloc( sizeof(char) * (nSeparators + 1) );
    
        i = 0;
        for(; i <= nSeparators; ++i)
        {
            *(results + i) = (char*) malloc( sizeof(char) * (*(length + i) + 1) );
            memcpy(*(results + i), str + start, *(length + i) + 1);
            *(*(results + i) + *(length + i)) = '\0';
            start += *(length + i) + 1;
        }
    
        free(length);
        *(results + nSeparators + 1) = NULL;
        return results;
    }
    It leaves some unfreed memory though.
    Last edited by rudyman; 04-24-2008 at 09:39 AM.

  5. #20
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    char** str_explode(const char separator, char* str)
    {
        int len = strlen(str);
        int nSeparators = 0;
    
        int* length = (int*) malloc( sizeof(int) * len );
    
        int i = 0;
        for(; i < len; ++i)
            *(length + i) = 0;
    
        i = 0;
        for(; i < len; ++i)
        {
            if( *(str + i) == separator )
                ++nSeparators;
            else
                *(length + nSeparators) += 1;
        }
    
        int start = 0;
        char** results = (char**) malloc( sizeof(char) * (nSeparators + 1) );
    
        i = 0;
        for(; i <= nSeparators; ++i)
        {
            *(results + i) = (char*) malloc( sizeof(char) * (*(length + i) + 1) );
            memcpy(*(results + i), str + start, *(length + i) + 1);
            *(*(results + i) + *(length + i)) = '\0';
            start += *(length + i) + 1;
        }
    
        free(length);
        *(results + nSeparators + 1) = NULL;
        return results;
    }
    Let's use ANSI C function style...
    I decided against this idea in C because it would mean you have to do return a 2D allocated array, which meant you had to free it later, which would be bad.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. parsing command line strings
    By John_L in forum C Programming
    Replies: 15
    Last Post: 05-28-2008, 08:26 AM
  2. sscanf and parsing strings
    By jco1323 in forum C Programming
    Replies: 4
    Last Post: 02-20-2008, 06:32 PM
  3. Parsing Strings
    By Hunter2 in forum C++ Programming
    Replies: 29
    Last Post: 12-05-2004, 07:48 PM
  4. Parsing Strings
    By SubLogic in forum C++ Programming
    Replies: 15
    Last Post: 01-07-2003, 11:11 AM
  5. Searching and Comparing Strings Using Parsing
    By niroopan in forum C++ Programming
    Replies: 3
    Last Post: 09-28-2002, 10:18 AM