Please can anybody help me? I'm a beginner .
 I need to write a c++ program that does the following :

1. Ask the user to enter two text file , the the first one contains a list of words in one column Regardless of their number , second one contains the text file    ,like this:
        //output

   Enter the keywords file: keywords_file.txt
   Enter the text file: text_file.txt

   2.Search for the keywords from the keywords file in the text file

    3.if the keyword exist the output =1 "true", if the keyword doesn't exist output =0 "false" ,like this :

    system  : 1  //its exist

   book : 0   //its doesn't exist



   4.  Then output in new text file (ofstream)


   I put the words in file each one on its own line because some of them are phrases I don't want to sprit them ,search them as one word , also the test            file I want it to stay as complete text not separate words from each other so possibly I cant use "map" & "vector". I already tried them...so possibly I can consider that each word in the words file just a line and read them all , then search for them in the text file

Dani AI

Generated

A simple, reliable approach that follows ’s advice to “break down the problem” is:

  • read the keywords file line-by-line into a container (a std::vector<std::string> is fine for phrases),
  • read the whole text file into a single std::string (preserving the text),
  • normalize newlines and case if desired,
  • search each keyword as a substring and write 1/0 to an output file.

The code below implements that: case-insensitive substring (phrase) matching, keeps phrases intact, replaces newlines with spaces so matches can cross lines, trims blank keyword lines, and writes results to results.txt.

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
#include <cctype>

static std::string trim(const std::string &s) {
    size_t a = s.find_first_not_of(" \t\r\n");
    if (a == std::string::npos) return "";
    size_t b = s.find_last_not_of(" \t\r\n");
    return s.substr(a, b - a + 1);
}

static std::string to_lower(std::string s) {
    std::transform(s.begin(), s.end(), s.begin(),
                   [](unsigned char c){ return std::tolower(c); });
    return s;
}

int main() {
    std::string kfile, tfile, outname = "results.txt";
    std::getline(std::cin, kfile); // assume prompts handled elsewhere
    std::getline(std::cin, tfile);

    std::ifstream kf(kfile);
    std::vector<std::string> keys;
    std::string line;
    while (std::getline(kf, line)) {
        line = trim(line);
        if (!line.empty()) keys.push_back(line);
    }

    std::ifstream tf(tfile);
    std::ostringstream ss;
    ss << tf.rdbuf();
    std::string text = ss.str();
    std::replace(text.begin(), text.end(), '\n', ' ');
    std::string text_l = to_lower(text);

    std::ofstream out(outname);
    for (const auto &kw : keys) {
        bool found = (text_l.find(to_lower(kw)) != std::string::npos);
        out << kw << " : " << (found ? 1 : 0) << '\n';
    }
    return 0;
}

Notes and pitfalls: std::string::find (see cppreference) performs substring matching. For whole-word matches use std::regex_search (cppreference) with escaped keywords; \b word boundaries may not cover punctuation cases. If the keyword list or text is very large, consider an Aho–Corasick based search for speed (Aho–Corasick algorithm). Handle CRLF, extra whitespace, and regex metacharacters in keywords when switching to regex.

Recommended Answers

All 2 Replies

This needs more definition. I think you meant "split" where you wrote "sprit." If I guessed wrong then I will be wrong here.

  1. I will not attempt to write your code for you. This is your assignment, not mine.
  2. You need to define that "split" carefully. Examples:
    a. "This matches?" in the keyword file does or does not have to search across lines in the text file.
    b. "This matches does have to match across linefeed/carriage returns (harder but not that much.)

Break down the problem to steps. For example you must be able to code on your own the first part where you read a line from your keyword file. Then you test that before you work out how to look for a match in the text file.

Do that first.

commented: yes , its split ,i will try with its , if i faced any problem will you help me? +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.