简体   繁体   中英

A function to find how many times string 1 contains string 2 c++

I want to check how many times my first string contains the second string. I read about it in the internet, and i found a function name std::find, i tried to use it and i failed...

std::string Str1 = "Hello Hello";
std::string Str2 = "ll";
Now what?

I tried to use

std::count

as well but i found out that its work just on a letters.

counter = std::count(Str1.begin(), Str2.end(), Str2); // dident work

Help??

Edit: Thats what i am trying to do:

unsigned int Nucleus::get_num_of_codon_appearances(const std::string& codon) const
{
    unsigned int counter = 0;
    counter = std::count(this->_DNA_strand.begin(), this->_DNA_strand.end(), codon);
    return counter;
}

You could do this quite easily with std::regex if you are using c++11 or greater.

Something like,

#include <regex>
#include <iterator>
#include <iostream>
#include <string>

using namespace std;

int main()
{
    const string str = "one two hello three four hello five hello";

    regex re("hello");
    cout << "Number of hellos : " <<  
        distance(sregex_iterator(str.begin(),str.end(), re),sregex_iterator());

}

Demo

You can use std::string::find.

#include <string>
using namespace std;

size_t count (const string & src, const string & str) {
    size_t cnt = 0, fnd = 0;
    while (fnd = (src.find(str, fnd)) != string::npos) {
        cnt++; fnd++;
    }
    return cnt;
}

...

count("Hello, world!", "ll");

As paxbun stated the string::find method is used there and that is a built-in function of the string class.

Reference of the .find() Method string::find ~ C++ Reference

As another approach including a class corresponding to your code above:

#include <iostream>
#include <string>

using namespace std;

//class declaration
class Nucleus{
private:
 string _DNA_strand{"ABCADDASDASABCAFGDACCACABCDA"};

public:
    const string get_codon(){return _DNA_strand;} //accessor of private variable
    unsigned int get_num_of_codon_appearances(const string& _DNA_strand, const string& ) const;
};

//Function  to return the number of times a string is found within another string.
unsigned int Nucleus::get_num_of_codon_appearances(const string& codon, const string& c) const
{
    unsigned int count = 0; //sets count
    size_t counter = 0; //sets counter
    while (counter != string::npos) // if counter does not equal string no position
    {
            size_t i = counter + c.length(); // sets i to counter + length of searched for object
            counter = codon.find(c, i); // .find() method 
            count++;
    }
    return count;
}

//Main Function
int main()
{
    Nucleus temp; //sets the object temp of the class Nucleus
    const string codon = temp.get_codon();
    const string c = "ABC";

    cout << "The Number of times " << c << " is found in " 
    << temp.get_codon() << " is: " << temp.get_num_of_codon_appearances(codon, c) << endl;

    return 0;   
}

Example output:

The Number of times ABC is found in ABCADDASDASABCAFGDACCACABCDA is: 3

DEMO

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM