|
-
Apr 24th, 2002, 05:40 PM
#1
Thread Starter
Lively Member
A find in string function
int findinstring(const char *inner, const char *outer, int start) {
int x = 0; // When it begins to count within the substring
bool searchflag = false; // To see if it's been found
for(int i = start; outer[i] != 0; i++) {
searchflag = true; // Sets it true, for now
for(x = 0; inner[x] != 0; x++)
if(outer[i + x] != inner[x])
searchflag = false;
/* ^ Checks to see if every occurence is true.
If but one is false, it will then set searchflag to false saying to continue the search. */
if (searchflag == true) // Tells it to quit the loop in this case, because we're now done.
break;
}
if (searchflag == true)
return i; // Returns pos if it could find one
else
return -1; // This is what it returns if it could not.
}
^ This above is a working function I created on my own. With this it will find the first occurence of a substring within a string from the point in the main string you have it start from. It will then return the first position of the first occurence of the substring it can find from your starting point, if it can find it; if it cannot find it from your starting point, then it will return -1.
- Justin Patrick Butler
Comme je trouve. "As I find."
- Butler family quote
Beneficia sumptos procul superant. "The benefits far exceed the costs."
- Myself
-
Apr 26th, 2002, 09:15 AM
#2
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Apr 26th, 2002, 11:19 AM
#3
PowerPoster
is there a question here somewhere or did you mean to post this as an answer to someone else's question?
-
Apr 26th, 2002, 03:22 PM
#4
Adding to Corned Bee's comment - look up strstr()
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|