Home > Back-end >  belonging of one string to another string
belonging of one string to another string

Time:10-19

I have this function that checks if a string is a substring, is it possible for me to add a variable to it that will count to me how many times that subsequence appears in that sequence? or i need to create another function for that.

bool SearchString(string sir1, string sir2) {
    
    if (sir2.size() > sir1.size())
        return false;
    for (int i = 0; i < sir1.size(); i  ) {
        int j = 0;
        if (sir1[i] == sir2[j]) {
            int k = i;
            while (sir1[i] == sir2[j] && j < sir2.size()) {
                j  ;
                i  ;
            }
            if (j == sir2.size())
                
                return true;
            else
                i = k;
            
        }
    }
    return false;
}

CodePudding user response:

For starters your function SearchString is too complicated and moreover can invoke undefined behavior in this loop

    while (sir1[i] == sir2[j] && j < sir2.size()) {
        j  ;
        i  ;
    }

if for example when the string s2 contains an imbedded zero character '\0'.

Also the function parameters should have constant referenced types.

The function can be written much simpler.

As for your question then it will be better to write a separate function to count occurrences of a sub-string.

Here is a demonstration program

#include <iostream>
#include <iomanip>
#include <string>

bool SearchString( const std::string &s1, const std::string &s2 )
{
    return s1.find( s2 ) != std::string::npos;
}

size_t CountStringOccurrences( const std::string &s1, const std::string &s2 )
{
    size_t n = 0;

    for ( std::string::size_type pos = 0; 
          s1.find( s2, pos ) != std::string::npos;
          pos  = s2.size() )
    {
          n;
    }

    return n;
}           

int main() 
{
    std::string s1( "123123123" );
    std::string s2( "123" );


    std::cout << std::boolalpha << SearchString( s1, s2 ) << '\n';
    std::cout << CountStringOccurrences( s1, s2 ) << '\n';

    return 0;
}

The program output is

true
3

CodePudding user response:

As "500 - Internal Server Error" said, you simply need to increment a counter where you return. With a little bit of refactoring it would look like this:

unsigned SearchString(const string& haystack, const string& needle) {
    if (needle.size() > haystack.size())
        return 0;
    unsigned count = 0;
    for (int i = 0; i < haystack.size();   i) {
        int j = 0;
        if (haystack[i] == needle[j]) {
            int k = i;
            while (k < haystack.size() && j < needle.size() && haystack[k] == needle[j]) {
                  j;
                  k;
            }
            if (j == needle.size())
                  count;
        }
    }
    return count;
}

Note: it's important to check that you haven't reached the end of the haystack while searching for the needle. Consider haystack="ababa", needle="bac": trying to locate 'c' would read after the end of the haystack It's also important to check reaching end before trying to dereference the next character:

while (sir1[i] == sir2[j] && j < sir2.size()) ...

would read sir2[j] before making sure j is not over the boundary.

  • Related