Q:

Write a C++ program to count the string "aa" in a given string and assume "aaa" contains two "aa"

0

Write a C++ program to count the string "aa" in a given string and assume "aaa" contains two "aa"

Sample Output:

2
2
3

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

#include <iostream>
using namespace std;


int test(string s)
        {
            int ctr_aa = 0;
            for (int i = 0; i < s.length() - 1; i++)
            {
                if (s.substr(i, 2) == "aa")
                {
                    ctr_aa++;
                }
            }
            return ctr_aa;
        }
        
int main() 
 {
  cout << test("bbaaccaag") << endl;  
  cout << test("jjkiaaasew") << endl;  
  cout << test("JSaaakoiaa") << endl;  
  return 0;    
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now