How to check how many times a substring occurs within a string?

FIFO1175

I am writing a function that searches for a substring in a given string and returns how many times the substring occurs in the string. For example, if the functions parameters were ("hellohello", "hel") the function would return 2. I apologize if the answer is obvious, I am relatively new to c++.

The if loop that i've created hasn't been working, and I'm not sure why. My function returns 0 when I call it in the main() function with its parameters set to ("hellohello", "hel").

int countMatches(string input, string substring)
{
    if(input == "" || substring == "")
    {
        return -1;
    }
    else
    {
        int matches;

        int substringLength = substring.length() - 1;

        for(int i = 0; i < input.length(); i++) 
        {
            if(input.substr(i, substringLength) == substring)
            {
                matches++;
                return matches;
            }
        }
    }

}

int main(){

    //test 1 
    //expected output 2
    cout << countMatches("hellohello", "hel") << endl;

}
Jerry Coffin

Your problem specification is slightly ambiguous. Can the substrings overlap? For example, if you're looking for the number of times AA occurs in AAAA, do you consider the correct answer to be 2 or 3?

Either way, we can use std::string::find to find the substrings. find takes a parameter to specify where it should start looking for a match. So, if we want to allow overlapping sub-strings, we search from the beginning, then each time we find a sub-string we re-start the search from one position after the beginning of the match we found.

If we only want to allow non-overlapping sub-strings, we start the same (search from the beginning) but when we find a substring we restart the search from just past the end of the substring we just find.

So, here's a version that'll count overlapping substrings:

size_t count_substrings(std::string const &needle, std::string const &haystack) {
    size_t count = 0;

    for (size_t pos =0; (pos=haystack.find(needle, pos)) != std::string::npos; ++pos, ++count)
        ;

   return count;
}

The modification necessary to count non-overlapping substrings is left as an exercise for the reader (but it honestly is pretty simple).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Check how many times a substring appears in a string

From Java

Determining how many times a substring occurs in a string in Python

From Dev

How to count how many times a string occurs

From Javascript

JavaScript: How many times a character occurs in a string?

From Dev

How can i check that how many times each element of splitted string occurs in each column?

From Dev

How many times there is a substring in a string [Java]

From Dev

How many times a string containes in substring - JavaScript

From Dev

Count how many times a time occurs within a date range

From Dev

String and substring and how many times substring present in main string

From Dev

How to find the number of times that a substring occurs in a given string (include jointed)?

From Dev

how to count how many times a string occurs in a column using pandas

From Dev

Numpy, How to count how many times a string occurs

From Dev

Make a function that shows how many times a word occurs in a string/sentence

From Dev

Check how many times a string appear in listbox

From Java

How to check # of times a string is present in each substring in a string?

From Dev

Find how many times a pair of values occurs

From Dev

How many times same date occurs

From Dev

Count how many times an object occurs in a list of a list within a DataFrame column

From Dev

Check how many times a string is contained in a string in Applescript

From Dev

How to check if a string contains a substring within an array of strings in Swift?

From Dev

How to check how many times character from string appears?

From Dev

How to check if int occurs in list 3 times?

From Dev

Comparing two lists (check how many times short list occurs in long list)

From Dev

Swift 4: How many times an element from a String array occurs in a String?

From Dev

Map of alphabet - how many times each letter occurs in a string (including 0)

From Dev

Count how many times a special character (non-spacing mark) occurs in a string in Swift

From Dev

How do I check whether or not a substring appears multiple times within a list in Python?

From Dev

How to find pattern match in text with exceptions if match occurs within a substring?

From Dev

Finding how many times a given string is a substring of another string using scala

Related Related

  1. 1

    Check how many times a substring appears in a string

  2. 2

    Determining how many times a substring occurs in a string in Python

  3. 3

    How to count how many times a string occurs

  4. 4

    JavaScript: How many times a character occurs in a string?

  5. 5

    How can i check that how many times each element of splitted string occurs in each column?

  6. 6

    How many times there is a substring in a string [Java]

  7. 7

    How many times a string containes in substring - JavaScript

  8. 8

    Count how many times a time occurs within a date range

  9. 9

    String and substring and how many times substring present in main string

  10. 10

    How to find the number of times that a substring occurs in a given string (include jointed)?

  11. 11

    how to count how many times a string occurs in a column using pandas

  12. 12

    Numpy, How to count how many times a string occurs

  13. 13

    Make a function that shows how many times a word occurs in a string/sentence

  14. 14

    Check how many times a string appear in listbox

  15. 15

    How to check # of times a string is present in each substring in a string?

  16. 16

    Find how many times a pair of values occurs

  17. 17

    How many times same date occurs

  18. 18

    Count how many times an object occurs in a list of a list within a DataFrame column

  19. 19

    Check how many times a string is contained in a string in Applescript

  20. 20

    How to check if a string contains a substring within an array of strings in Swift?

  21. 21

    How to check how many times character from string appears?

  22. 22

    How to check if int occurs in list 3 times?

  23. 23

    Comparing two lists (check how many times short list occurs in long list)

  24. 24

    Swift 4: How many times an element from a String array occurs in a String?

  25. 25

    Map of alphabet - how many times each letter occurs in a string (including 0)

  26. 26

    Count how many times a special character (non-spacing mark) occurs in a string in Swift

  27. 27

    How do I check whether or not a substring appears multiple times within a list in Python?

  28. 28

    How to find pattern match in text with exceptions if match occurs within a substring?

  29. 29

    Finding how many times a given string is a substring of another string using scala

HotTag

Archive