/
bananas.cpp
42 lines (36 loc) · 1.09 KB
/
bananas.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <iostream>
#include <fstream>
#include <sstream>
#include <cstdlib>
#include <string>
using namespace std;
int bananas(string banana){
string str2="banana";
ifstream myFile;
myFile.open(banana.c_str());
string line;
size_t found;
while ( getline(myFile, line) ){
found = line.find(str2);
if (found!=string::npos)
cout << "first 'banana' found at: " <<found<<endl;
found=line.find("banana",found+1);
if (found!=string::npos)
cout << "second 'banana' found at: " << found <<endl;
found=line.find("banana",found+2);
if (found!=string::npos)
cout << "third 'banana' found at: " << found <<endl;
found=line.find("banana",found+4);
if (found!=string::npos)
cout << "forth 'banana' found at: " << found <<endl;
found=line.find("banana",found+5);
if (found!=string::npos)
cout << "fifth 'banana' found at: " << found <<endl;
}
myFile.close();
return 0;
}
int main(){
bananas("bananas.txt");
}