fork download
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. //class declaration
  7. class Nucleus{
  8. private:
  9. string _DNA_strand{"ABCADDASDASABCAFGDACCACABCDA"};
  10.  
  11. public:
  12. const string get_codon(){return _DNA_strand;} //accessor of private variable
  13. unsigned int get_num_of_codon_appearances(const string& _DNA_strand, const string& ) const;
  14. };
  15.  
  16. //Function to return the number of times a string is found within another string.
  17. unsigned int Nucleus::get_num_of_codon_appearances(const string& codon, const string& c) const
  18. {
  19. unsigned int count = 0; //sets count
  20. size_t counter = 0; //sets counter
  21. while (counter != string::npos) // if counter does not equal string no position
  22. {
  23. size_t i = counter + c.length(); // sets i to counter + length of searched for object
  24. counter = codon.find(c, i); // .find() method
  25. count++;
  26. }
  27. return count;
  28. }
  29.  
  30. //Main Function
  31. int main()
  32. {
  33. Nucleus temp;
  34. const string codon = temp.get_codon();
  35. const string c = "ABC";
  36.  
  37. cout << "The Number of times " << c << " is found in "
  38. << temp.get_codon() << " is: " << temp.get_num_of_codon_appearances(codon, c) << endl;
  39.  
  40. return 0;
  41. }
  42.  
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
The Number of times ABC is found in ABCADDASDASABCAFGDACCACABCDA is: 3