-1

I need to print all words from the text file which includes substring, how can I add to the code? The code which I have is printed below

#include <iostream>
#include <fstream>
#include <bits/stdc++.h>
using namespace std;


int main()
{
   ifstream f("lab.txt");
   fstream n("lab2.txt", ios::out);
   if (f.is_open()) {
       string s;
       string word = "";
       string substring ="ab"

       while(getline(f, s)) {
           for (auto x : s)
           {
               if (x == ' '|| x==','|| x=='!'|| x=='?'|| x==':'|| x==')'|| x=='(')
               {
                   n << word << endl;
                   word = "";
               }
               else {
                   word = word + x;
               }
           }
           n << word << endl;
           word="";
       }
    }
}
mch
  • 8,786
  • 2
  • 29
  • 42
  • 2
    `if(word.find(substring) != std::string::npos) { ... }`? – Aconcagua May 13 '22 at 07:44
  • 1
    You don't need use getline, `while (std::cout >> word)` is very well. If substring is found in word, trim punctuations from word and output. – 273K May 13 '22 at 07:44
  • 1
    You shouldn't ever [`#include `](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h). – Aconcagua May 13 '22 at 07:45
  • 1
    About [`using namespace std`](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – Aconcagua May 13 '22 at 07:46
  • Thank you, it worked, and maybe somebody knows how to add that a word should be not longer then 40 characters, if it is more then 40 then new word starts – Veronika Novikova May 13 '22 at 10:26

0 Answers0