Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

strstr modified - need function

Status
Not open for further replies.

cyprus106

Programmer
Apr 30, 2001
654
i need a function that searches only so far in a string, and searches for a certain phrase, or set of characters. this is just like strstr, but it only searches a designated part of the string. i.e.:
if my phrase was "borland C++ is great" and i wanted to search for 'great' between the 9th characterand the last character, i need to know how to make a function that does that, but i don't know how, any sugestions? Cyprus
 
in this case it is fairly easy

char s1[50]={"borland C++ is great"} ;
char s2[50]={"great"};

char *ptr =strstr(s1+9,s2);
strstr searches for s2 in string s1+9, which is the string from position 9.

if you want to search in a part of a string in between you can make a function like this:


void Strstr(char *s1,char *s2,int start,int end,char *ptr)
{ // start is the startposition of the substring
// 0 is the first position
// end is the last position of the substring I want to
// search in

char[200]tempstring;
ptr=NULL;
if(end<=(start+1)) return;
if(end>strlen(s1)) return;
if(start<0) return;
strcpy(tempstring,s1+start);
if(end!=strlen(s1))
tempstring[end-start+1]=0;
// make a temporary string
ptr=strstr(tempstring,s2);
// search for occurence of s2 in tempstring
if(ptr)
ptr=s1+start+(ptr-tempstring);
// calculate the offset from within the original string
// tempstring is a temporary string that is destroyed
// after this function is done

return ;
} // ptr is the pointer to the part of the substring
// where the searchstring starts


Is this what you want








Wim Vanherp
Wim.Vanherp@myself.com
 
yeah....yeah! thanx! i didn't have the time nor the patience to deal with trying to make that, and for a while after I posted this it looked like noone else did either. thanks, alot. it's appreciated Cyprus
 
I started to answer this post the other day but didn't finish. I see someone else gave you a solution. The solution I was going to give used Borland's AnsiString functions. This is a simple two liner. It may run a tad slower than winvanherp's solution but only negligably.

{
int startchar;
String Original, Temp;

Original = &quot;borland C++ is great&quot;;
Temp = Original.SubString(9, Original.Length()-9);
startchar = Temp.Pos(&quot;great&quot;);

//startchar should equal 8

}

I didn't actually compile this but I'm sure its close enough that you can understand whats going on.

Not as elegant as the c function but I find it gets the job done.

If you want a good description of the AnsiString class see the tutorials section at
Don Smith
 
hey, great, thanx! I'll use it, thanx for the solutions, guys Cyprus
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top