hey guys i need help with my code ; well I am required to write a program that uses getline to read a string, counts the number of occurrences of the substring “is” in this string, and replaces all occurrences of “is” by “at” . I've written the code but there seems to be an error in replacing "is" and "it".
#include <iostream>
#include <string>
usingnamespace std;
int main() {
int length=0, position=0;
int counter=1;
string statement ;
string string1 = " is ";
string string2 = " at ";
cout << " Enter a statement : " << endl;
getline(cin , statement);
length = statement.length();
position = statement.find(string1);
while ( position != string::npos ) {
for ( int i = 0 ; i < string2.length() ; i++) {
statement.replace(position, string1.length() , string2 );
}
counter++;
position = statement.find( string1 , position + string1.length() );
}
cout << "The number of occurance of \"is\" is :" << counter << endl;
cout << "The new statement after being altered is : " << statement << endl;
system ("pause");
return 0;
}