Thread: Splitting a string?

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    28

    Splitting a string?

    Hello! I have a question. I'm trying to take this string:

    howOld:20
    and split it, right at the ":", so I get an Array like this:

    Code:
    myArray[1] = {"howOld","20"}
    How difficult is this to do, and how should I go about doing it? I'm reading that string from a settings file.

    Thanks in advance!

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Look into the strtok() function in string.h. You specify a string and a delimiter (in your case the ':') every time you call it, and each time you call it, it will return a pointer to the next piece of the string. When there are no more pieces, it returns NULL.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    another way - is using sscanf - which will not modify original buffer, but you will need to provide the additional storage for the parsed substrings
    Code:
    char name[20];
    char val[11];
    const char* str="howOld:20";
    if(sscanf(str,"%19[^:]:%10[^\n]", name, val) == 2)
    {
       /* parsed ok*/
    }
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Registered User Maz's Avatar
    Join Date
    Nov 2005
    Location
    Finland
    Posts
    194
    there is lot of solutions. Here was another thread with similar question, and i suggested these. 1. Loop through the array, char by char, and replace each : with \0. And store start address for next string in ptr array. 2. I wrote c version of php's explode, and it is introduced in my blog. It should do what you want. Link is in my signature.

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    28
    Before I read either of your posts (I had left the house) a little mental brainstorming helped me to come up with this:

    Code:
    char name[20], value[20];
    
    static int parseSetting(char* myString)
    {
            int i, array[1], i2 = 0, len = strlen(myString);
    	bool sep = false;
    	
    	for(i = 0; i<len; i++){
    	    if (myString[i] != ':') {
    		    if(!sep){
    		        name[i] = myString[i];
    			} else {
    			    value[i2] = myString[i];
    				i2++;
    			}
    		} else {
    		    sep = true;
    		}
    	}
    	
    	name[i] = '\0';
    	value[i] = '\0';
    	
    	array[0] = name;
    	array[1] = value;
    	
    	return array;
    }
    
    parseSetting("howOld:20");
    The bad thing is that it gives these warnings:

    In function 'parseSetting':
    warning: assignment makes integer from pointer without a cast
    warning: assignment makes integer from pointer without a cast
    warning: return makes integer from pointer without a cast
    warning: function returns address of local variable
    What's wrong?

  6. #6
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    name is a character array, and when you use just the array name, the compiler evaluates it as a pointer to the first element in the array. So you're assigning a pointer (&name[0]) to an integer (array[0]). If you want to get a number out of a string you should use atoi(), or sscanf().

    Another problem I see is that you declare an array as "int array[1]" - which only has one element. If you declare "int array[2]" you can refer to the two elements as array[0] and array[1].

  7. #7
    Registered User
    Join Date
    Mar 2009
    Posts
    28
    Quote Originally Posted by sean View Post
    name is a character array, and when you use just the array name, the compiler evaluates it as a pointer to the first element in the array. So you're assigning a pointer (&name[0]) to an integer (array[0]). If you want to get a number out of a string you should use atoi(), or sscanf().

    Another problem I see is that you declare an array as "int array[1]" - which only has one element. If you declare "int array[2]" you can refer to the two elements as array[0] and array[1].
    Hmm...should I do a for loop instead of a plain assignment?

  8. #8
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Unless you're doing more than 2 or 3 elements I hardly see the point. It wouldn't fix any of your errors.

  9. #9
    Registered User
    Join Date
    Mar 2009
    Posts
    28
    Code:
    array[0] = name;
    array[1] = value;
    I'm just asking what I should do to get this to work. The 'value' variable isn't always going to be a number.

  10. #10
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Then I would store it as a string. Maybe you should have an array of char pointers.

  11. #11
    Registered User
    Join Date
    Mar 2009
    Posts
    28
    Huh? I'm not sure I'm catching you. Store what as a string?

  12. #12
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    If it doesn't store integers, why is array define an array of ints?

    you can declare an array of two char pointer like this:
    Code:
    char *charptrs[2];
    This will require you to make sure that the char pointers are properly managed and freed, if need be. The easiest way is to malloc memory, and copy the strings over.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  13. #13
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    If the data can be anything, just store it in the format that you originally recieved it in: a string. Since you're splitting it into parts, you need an array of strings (or just two separate strings). The way you can do that is

    Code:
    char * strings[2];
    strings[0] = &first_string[0]; // You set the pointer to the address of the first element
    strings[1] = second_string; // You can refer to an array by name too, and you get the address of the first element

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  2. String splitting algorithm help
    By (TNT) in forum C++ Programming
    Replies: 7
    Last Post: 05-12-2007, 11:28 AM
  3. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM