Substr

This is a discussion on Substr within the C Programming forums, part of the General Programming Boards category; Is there no equivalent to the substr function in c++ in c???...

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    23

    Substr

    Is there no equivalent to the substr function in c++ in c???

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,801
    You can use the sprintf function to simulate that.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    I think he means strstr(), which finds a string within a string.

    To extract a substring from a string, use strncpy, as in
    strncpy( dest, &src[start], len );
    dest[len] = '\0';
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    I took hk's reply in this manner:
    Code:
    #include <stdio.h>
    
    char *substr(char *dst, const char *src, int start, int len)
    {
       sprintf(dst, "%.*s", len, &src[start]);
       return dst;
    }
    
    int main(void)
    {
       const char src[] = "Here is some text.";
       char dst[sizeof src];
       printf("dst = \"%s\"\n", substr(dst,src,5,7));
       return 0;
    }
    
    /* my output
    dst = "is some"
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Mad OnionKnight's Avatar
    Join Date
    Jan 2005
    Location
    Umeň, Sweden
    Posts
    555
    There's no substr function in the C standard libraries. The standard libraries don't like using dynamic memory with strdup as the only exception I can think of. C++ has a bunch though, I don't know why but I suspect it's because they don't want to give people the burden of having to free memory themselves.

    I wrote a substr function once which was supposed to be like the Perl substr but there were some limitations since I couldn't replace text on a string of unknown size so I had the function make a copy instead and that 0 and NULL is used to signal unused parameters.

    Code:
    #include <stdlib.h>
    
    char* substr (const char* string, int pos, int len, const char* replace)
    {
    	char* substring;
    	int   i;
    	int   length;
    
    	if (string == NULL)
    		return NULL;
    	length = strlen(string);
    	if (pos < 0) {
    		pos = length + pos;
    		if (pos < 0) pos = 0;
    	}
    	else if (pos > length) pos = length;
    	if (len <= 0) {
    		len = length - pos + len;
    		if (len < 0) len = length - pos;
    	}
    	if (pos + len > length) len = length - pos;
    	if (replace != NULL) {
    		if ((substring = malloc(sizeof(*substring)*(length-len+strlen(replace)+1))) == NULL)
    			return NULL;
    		for (i = 0; i != pos; i++) substring[i] = string[i];
    		pos = pos + len;
    		for (len = 0; replace[len]; i++, len++) substring[i] = replace[len];
    		for (; string[pos]; pos++, i++) substring[i] = string[pos];
    		substring[i] = '\0';
    	}
    	else {
    		if ((substring = malloc(sizeof(*substring)*(len+1))) == NULL)
    			return NULL;
    		len += pos;
    		for (i = 0; pos != len; i++, pos++)
    			substring[i] = string[pos];
    		substring[i] = '\0';
    	}
    
    	return substring;
    }
    substr("some string", 5, 0, NULL)
    returns "string"

    substr("some string", -5, 3, NULL)
    returns "str"

    substr("some string", 4, 0, "thing")
    returns "something"

    free() needs to be called on the string returned after it's use.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. substr problems
    By spudval in forum C++ Programming
    Replies: 4
    Last Post: 06-26-2007, 05:48 AM
  2. something's up with my substr() test
    By indigo0086 in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2006, 06:50 AM
  3. char * substr
    By dfom_2004 in forum C++ Programming
    Replies: 8
    Last Post: 04-21-2006, 07:25 PM
  4. substr question
    By gustavosserra in forum C++ Programming
    Replies: 5
    Last Post: 08-30-2003, 07:44 PM
  5. using substr
    By riley03 in forum C++ Programming
    Replies: 2
    Last Post: 02-24-2002, 05:32 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21