#include <iostream>
#include <fstream>
using namespace std;
int main(){
ofstream os("test.txt");
os << "1. Hi." << endl <<"2. By mom!" << endl << endl << "4. YES I did it." << endl << "5. I'm fine." << endl << "6. I'm writing different lines to the file, OK?";
os.close();
cout << "My file contains following data:\n";
cout << "1. Hi." << endl <<"2. By mom!" << endl << endl << "4. YES I did it." << endl << "5. I'm fine." << endl << "6. I'm writing different lines to the file, OK?";
cout << "\n\nEnter a word to find from it: ";
char word[50]; cin.getline(word,50,'\n');
//Calculating number of characters in word.
for ( int sizeOfWord = 0; word[sizeOfWord]!='\0'; sizeOfWord++);
//searching the word.
ifstream is("test.txt");
char sentence[200];
int line_no = 1;
int i = 0;
bool found = false;
while (!is.eof()){
is.getline(sentence,200,'\n');
for (int j = 0; sentence[j]!='\0'; j++){
if (sentence[j] == word[i]){
++i;
}
else{
i = 0;
}
if (i >= sizeOfWord){
cout << "Found at line: " << line_no << endl;
found = true;
}
}
line_no++;
}
if (!found){
cout << "NOT found." << endl;
}
return 0;
}
No comments:
Post a Comment