I've made a program that counts the number of times a substring appears within a string.It works just fine if the string has no spaces but if it has any it doesn't count the substrings after the first space.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include<iostream>
#include<string>
usingnamespace std;
int main()
{
int count = 0;
string x;
cin >> x;
for (int i = 0; i < x.size() - 1; i++) {
if ( x.substr(i,2) == "am") count++;
}
cout << count;
return 0;
}
Is it that size() only tells you the size of the string until it "hits" a space?If yes then how do I get the actual length of a string?