Substring in C?



  • I am fairly new to C and need to extract a certain substring from a string of characters. I have read two books about C, fiddled around a bit with Nintendo DS programming, but haven't gotten around to do stuff like this before, so I hope someone can help me out a bit here. :) The string I have is like:


    Number: "474305724856"

    I want to extract the number (474305724856) from that in a fresh character array (I know the number is a maximum of 20 characters long if correct). The simple solution I came up with was (where 'buff' is a char* containing the mentioned text):

    char nr[20];

    char *start = strchr(buff, '"') + 1;

    strncpy(nr, start, strchr(start, '"') - start);



    This works, but as I see it this is far from optimal. Several possible erroneous cases are left unhandeled: what if start is NULL (ie no quotes are found to begin with), what if there is one quote, which is the last character of the string, what if the resulting substring is more than 20 characters long?

    I can check start for NULL, check if start+1 < strlen(buff), check if the 2nd strchr (let's call it char *end) minus start is < 20. This all feels like I'm reinventing the wheel though.

    Any suggestions welcome. :)



  • What the hell are you talking about? the string literally has a quotation mark as the first character? 

    also you are using the last parameter of strncpy wrong.  That should be the MAXIMUM size, not the length of the string to copy.  so just put 20 there.

    anyways, if you are wanting to get the string within the quotation marks you should try strsep.



  • @pbean said:

    I know the number is a maximum of 20 characters long if correct

    That means you'll need an array of 21 bytes, unless non-NUL-terminated strings are okay. Your current code will overflow the buffer if the number is invalid. I suggest to check that both quotes are in place and the number is acceptable, then memcpy the substring and pad it with a NUL.



  • @tster said:

    What the hell are you talking about?


    About substracting a substring from a string.

    @tster said:

    the string literally has a quotation mark as the first character?

    The string is, as I stated:

    Number: "457389457934"



    (In which the quotation marks actually appear in the string).

    @tster said:

    also you are using the last parameter of strncpy wrong.  That should be the MAXIMUM size, not the length of the string to copy.  so just put 20 there.

    What's the difference between the maximum number of characters and the length of the string I want to copy? In above sample, the number is 12 characters long, so I want to copy 12 characters. If I would fill in 20, it would copy the quotation mark as well. Or have I misunderstood it once again?

    @tster said:

    anyways, if you are wanting to get the string within the quotation marks you should try strsep.

    Thanks, I will look into this function.

    @Spectre said:

    That means you'll need an array of 21 bytes, unless non-NUL-terminated strings are okay. Your current code will overflow the buffer if the number is invalid.

    I totally forgot about null termination, so thanks for the warning. Indeed it will need to be 21 bytes in length.



  • @pbean said:

    What's the difference between the maximum number of characters and the length of the string I want to copy? In above sample, the number is 12 characters long, so I want to copy 12 characters. If I would fill in 20, it would copy the quotation mark as well. Or have I misunderstood it once again?
     

    The idea behind the family of functions like strncpy() is to set an absolute upper limit to the amount of string to copy.  If you only bufferred 20 characters, and for some reason, you requested to copy 25 characters, the function will only do 20 characters so as to prevent the overwriting of other memory not associated with the allocated buffer.

    Many vulnerabilities and bugs in software can be traced back to some portion of code that did not respect the amount of memory allocated...



  •  Also, if you are planing on eventually turning the substring of numbers into an actual integer, may I suggest using sscanf() on the original string instead of some sort of combination of substringing and atoi()?


Log in to reply