Q:

Write a C++ program to check whether the characters e and g are separated by exactly 2 places anywhere in a given string at least once

0

Write a C++ program to check whether the characters e and g are separated by exactly 2 places anywhere in a given string at least once

Sample Output:

Original string: eagerer -> 1

Original string: eaglets -> 1

Original string: eardrop -> 0

All Answers

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

#include <iostream>

using namespace std;

bool Check_chars(string text) {

  int len = int(text.size());
  for (int i = 0; i < len; i++){
    if (text[i] == 'e' || text[i] == 'E'){
      if (i+2 < len  && (text[i+2] == 'g' || text[i+2] == 'G'))
          return true;
    }
    if (text[i] == 'g' || text[i] == 'G'){
      if (i+2 < len  && (text[i+2] == 'e' || text[i+2] == 'e'))
          return true;
    }
  }
  return false;

}

    int main() {
        cout << "Original string: eagerer -> " << Check_chars("eagerer") << endl;
        cout << "\nOriginal string: eaglets -> " << Check_chars("eaglets") << endl;
        cout << "\nOriginal string: eardrop -> " << Check_chars("eardrop") << endl;
        cout << "\nOriginal string: After eagling the Road Hole on Thursday, he missed an 8-footer for birdie Friday. -> ";
		cout << Check_chars("After eagling the Road Hole on Thursday, he missed an 8-footer for birdie Friday.") << 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