const char * strstr ( const char * str1, const char * str2 );
char * strstr ( char * str1, const char * str2 );

Locate substring

Returns a pointer to the first occurrence of str2 in str1, or a null pointer if there str2 is not part of str1.

The matching process does not include the terminating null-characters.

How 2 do the same thing without using the strstr function?

>How 2 do the same thing without using the strstr function?
Write your own. Since a string is really a char array, you're going to need to create some sort of loop.

It should:

  • Loop for the entire length of the first string (the one that you're searching)
  • Update a second counter which is used for the second string
  • If that second counter hits the end of the second string, you should exit
  • Anytime the characters don't match, you should reset that counter

But please post your own attempt at writing one first...

#include<iostream>
using namespace std;

void main(){
    char a[] = "Bobbie is an animal";
    char b[] = "is";
    char* c;
    int z = 0;

    for(int x = 0; x < (strlen(a)+1); x++){
        if((a[x]==b[z])&&(z==0)){
                c = &a[x];
        }
        z++;
        if(a[x]!=b[z])z = 0;
        if((a[x]==b[z])&&(b[z]=='\0'))goto end;
                }
end:
                cout<<c<<endl;

}

help me fix my program... it will search for the last "i" instread of "is".

Hmm, you're on the right track.

First of all, please use code tags. Now for your code:

#include<iostream>
using namespace std;

void main(){ // don't use void main, int main is much better
    char a[] = "Bobbie is an animal";
    char b[] = "is";
    char* c;
    int z = 0;

    for(int x = 0; x < (strlen(a)+1); x++){

// how about incrementing 'z' here if we find a match?
        if((a[x]==b[z])&&(z==0)){
                c = &a[x];
        }
        z++;
// you could write this as an else statement to the previous if ()
        if(a[x]!=b[z])z = 0;

// don't use gotos, 'break' is a better alternative
// also, why don't you just check to see if 'z' is larger than or equal to
// length of 'b'? It's a much simpler statement
        if((a[x]==b[z])&&(b[z]=='\0'))goto end;
                }
end:
                cout<<c<<endl;

}

Another thing that I'd advise is that you declare 'i' before the loop (so it doesn't go out of scope), and then instead of pointing 'c' in the loop, point it afterwards like so:

// might seem a little complicated...
// you're moving 'a' over by the number of spaces you took to find 'b'
// then you move back by 'z' to get to the start of the string that you were looking for
// lastly, you add 1 to compensate for arrays starting at 0
c = a+x-z+1;

First thing is for(int x = 0; x < (strlen(a)+1); x++){ will blow your array boundaries. You definitely don't need the +1 -- and probably want something like x < (strlen(a)-strlen(b)) instead.

Then you need a loop within a loop:
Search through a looking for 1st character in b.
When you find one, start another loop that tests the next values in a to see if they are the same as the rest of b

3rd, give your variables better name. Learn to make the names have meaning -- it will help as time goes on.

Last, use CODE tags.

Silly me. I was trying to optimize my algorithm by avoiding nested loops, thinking that it would waste time searching the same chars over and over again. But I forgot that it would fail in a scenario like this:

first string = "seearching string"
second string = "earch"

Because of the double 'e's, the program would totally miss it without backing up to recheck them. So, disregard the algorithm I gave previously. Use WaltP's. ;)

hmm...what is code tags?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.