the use of string methods

Pages: 12
It is ok if you don't fully understand what size_t is, but in this case you should use it even so, or use auto.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>

using namespace std;

int main()
{
	string needle = "needle";
	string haystack = "fgndndndtjnedj";
	int needle_appearances = 0;
	size_t i = 0;
	cout << "Enter the haystack: \n";
	getline(cin, haystack, '\n');
	cout << "Enter the needle: \n";
	cin >> needle;
	for (i = haystack.find(needle, i); i != string::npos; i = haystack.find(needle, i))
	{
		needle_appearances++;
		i++;
	}
	cout << "The word " << needle << " has appeared " << needle_appearances << " times in your haystack.";

	return 0;
}
Last edited on
ok guys the the biggest issue was my 18th line of code... once put "i=" the program starded working properly and quickly enough. As regard size_t hopefully I m going to study it soon :)
Topic archived. No new replies allowed.
Pages: 12