0

Im trying to write a function that returns the index of the first occurence of a substring within a string.

like searching skyscraper for 'ysc' would return 2.

It seems like strstr is doing its job because after the while loop it outputs the correct first char of the substring but it isnt counting correctly and returning the correct int.

I may have this set up wrong as I am passing an array to the function and got a lot of errors trying to use nested loops so I tried for a while loop instead which compiles but isnt outputting correctly.

Im still fairly new to pointers and passing them as arguements so there may be an issue there.

anything helps!

int findSubstring(char *s, char substring[])
{   
    int j = 1;
    char* strPtr = NULL;

    while (strPtr == NULL)
    {
        strPtr = strstr(s, substring);
        if (strPtr != NULL)
            break;
        j++;
    }

    cout << *strPtr << endl;
    cout << "The substring begins at element: " << j << endl;
    return j;
}
John
  • 15,990
  • 10
  • 70
  • 110
John Park
  • 290
  • 2
  • 8
  • 25

3 Answers3

0

You seem to be over complicating the task, since you're using C++ you should be using std::string::find.

std::string s = "skyscraper";
std::size_t pos = s.find("ysc");
if( pos != std::string::npos )
    std::cout << "ysc found at " << pos << "\n";
else
    std::cout << "ysc not found" << "\n";
Drew Chapin
  • 7,779
  • 5
  • 58
  • 84
0

What about just using pointer arithmetic?

int findSubstring(char *s, char substring[])
{   
    char* strPtr = strstr(s, substring);
    if (strPtr != NULL)
    {
        return (int)(strPtr - s);
    }
    else
    {
        return -1;  //no match
    }
}
John
  • 15,990
  • 10
  • 70
  • 110
  • return (int)(strPtr - s); could you explain this line to me? This code works exactly how i want it too but i'd like to know why im using it! – John Park Oct 04 '13 at 20:04
  • Character pointers hold addresses to characters. Taking the difference of two character pointers tells you how many characters you move through going from one to the other. Since `strPtr` points to one of the characters in `s`, it will tell you the number of characters *in that string* that you went through. Casting the difference to an integer makes sure that your return type is correct. – John Oct 04 '13 at 20:18
  • Thank you, the casting part seemed obvious but i didnt get the pointer subtraction. That makes sense now. – John Park Oct 04 '13 at 20:36
0

Fix yours as, refer this example:

 int findSubstring(char *s, char substring[]) 
{
    char* pos = strstr(s, substring);
    if(pos) 
     return (int)(pos - s);
    else -1;
}

And you're using C++, so, why not std::string ?

 int findSubstring(const std::string& s, const std::string& substring)
{   

std::size_t j =s.find(substring);
return ( j!=std::string::npos) ? j :-1;
}
P0W
  • 46,614
  • 9
  • 72
  • 119
  • return (int)(pos - s); could you explain how this line works? also im completely unfamiliar with using std::(anything) but Im seeing it a lot and feel like I should look into using that syntax instead. – John Park Oct 04 '13 at 20:13