Advertisement

How To Pull A Substring Out Of A Char * String?

Started by July 28, 2000 04:17 PM
11 comments, last by TipTup 24 years, 4 months ago
    
char *substr(char *string, int begin, int end){
	char temp[256]="";
	for(int i=begin; i<end; i++){
		strcat(temp, (char *)string<i>);
	}
	return temp;
}
    
That is what I tried, basically I''m trying to take data from a socket and pull out parts of it at a time. I tried searching through all the strXXX functions and could not find one to pull substrings out (just find where they begin) Any ideas? this doesnt work, lol. -TipTup TipTup.Com
In the strcat function it changed [] to <>

-TipTup
TipTup.Com
Advertisement
Your problem is that string is not a string, but a single character. So your call to strcat is going to fail since it''s second parameter is expected to be a null-terminated string, which it isn''t. To do substr try this:

char* substr(char* string, int begin, int end)
{
char* temp = new char[end-begin+1];
strncpy(temp, string+begin, end-begin+1);
temp[end-begin+1] = ''\0'';
return temp;
}

Try this:

    char *substr(char *string, int begin, int end){char temp[256] = "";int i=0;while(begin < end)  // go into assignment loop{temp[i++] = string[begin++];  // assign them}// now append a terminatortemp<i> = ''\0'';return(temp);  // give us back the string}    


I havent tested that, I just made it up off the top of my head but I think it should work.
There are probably better designed ways to extract a substring - there might even be a library function but I don''t recall it right now.
I''m not even sure that code looks right. But there we go.

-Mezz
Both of those didn''t work.. The second one (strncpy) returned a few alt characters but didnt return the substring... Any other ideas?
BTW plz dont (source)(/source) it, its hard to c&p (it merges everything to one line)

-TipTup
TipTup.Com
strstr is a function that searches for strings within a string.

-----------------------------
-cow_in_the_well

http://cowswell.gda.ods.org/


"If it's stupid but works, it's not stupid" - Murphey's Law

- Thomas Cowellwebsite | journal | engine video

Advertisement
It searches but I''m trying to return a substring using numbers for place in the string
ie. return characters 10-20

-TipTup
TipTup.Com
    /* Should work, I think */char* substr(char *string, int begin, int end){char substring[end - begin];int i = begin;strcat(substring, for(;string[i++]<img src="wink.gif" width=15 height=15 align=middle>);return substring; }    


-----------------------------

A wise man once said "A person with half a clue is more dangerous than a person with or without one."
That was me, but I dont think it works anyway. Try this:

    char *substr(char *string, int begin, int end){ char substring[end - begin]; char i = begin; if ((end < 0) || (begin < 0) || (end < begin)) return NULL; return (for(;strcat(substring , string<i>), i++; ));}    


-----------------------------

A wise man once said "A person with half a clue is more dangerous than a person with or without one."

Edited by - immaGNUman on July 28, 2000 12:52:12 AM
-----------------------------A wise man once said "A person with half a clue is more dangerous than a person with or without one."The Micro$haft BSOD T-Shirt
    void substr(char *string, int begin, int end, char *result){	result = (char *)malloc(end-begin+1);	strncpy(result, string+begin, end-begin+1);	result[end-begin] = 0;}    


I''m making progress with that but it still isn''t fully accurate.. lol

-TipTup
TipTup.Com

This topic is closed to new replies.

Advertisement