Write a C program that allows the user to search a file MyTe

Write a C++ program that allows the user to search a file “MyText.txt” located in the current workspace for any word (String). The program should print the result of the search. In the event the word is found the program should indicate the line where it was found in the text file.

Example:

Enter the word you are looking for: Andi

The word Andi was not found!

Or

Enter the word you are looking for: Andi

The word Andi was found in line 3 of the text file!

Solution

SOURCE CODE:

#include<iostream>
#include<fstream>
#include<cstring>
using namespace std;

int main()
{
int val, num = 0;
string str=\"\",line=\"\";
  
cout << \"Enter the word you are looking for: \";
cin >> str;
  
ifstream myfile(\"MyText.txt\");
   if (myfile.is_open())
   {
while ( getline (myfile,line) )
{
   num++;
  
   if(strstr(line.c_str(),str.c_str())!=NULL)
   {
       cout<<\"The word \"<<str<<\" was found in line \"<<num<<\" of the text file!\"<<endl;
       num=0;
       break;
       }
}
  
if(num != 0)
{
   cout<<\"The word \"<<str<<\" was not found!\"<<endl;
   }
myfile.close();
}

  
}

OUTPUT:

Enter the word you are looking for: scanf
The word scanf was found in line 13 of the text file!

Enter the word you are looking for: Andi
The word Andi was not found

--------------------------------------------------------------------------------------------------------------------------

MyText.txt

#include<stdio.h>
int s(int a,int b)
{
   if(a==1)
       return b;
   else
       return 4*s(a-1,b);
}

int main()
{
       int a,b;
       scanf(\"%d     %d\",&a,&b);
       printf(\"\ %d\",s(a,b));
       return 0;
}

Write a C++ program that allows the user to search a file “MyText.txt” located in the current workspace for any word (String). The program should print the resu
Write a C++ program that allows the user to search a file “MyText.txt” located in the current workspace for any word (String). The program should print the resu

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site