hi,

for( turbo c++)

:eek: is there anyway my program can recognize words :idea: :!: . for eg.

cin.getline(sentence,10); :rolleyes:

"Hello how are you?"

then the program recognises the words - hello, how, are , you and prints "im fine". :)

and if i cin >> " hello how are you, im fine"

and it recognises words " hello , how ,are , you" and print the same messege just by recognising the words in the sentence.

,
cpp noob.

Dani AI

Generated

For who wants a program to "recognize" words and reply: first decide whether you need simple trigger matching or full language understanding, as pointed out. For many small programs a reliable pipeline is: normalize text (lowercase), strip punctuation, split into tokens, then test tokens or whole‑phrase patterns. Normalization avoids misses from case and stray punctuation.

Tokenization example (portable C++11+): normalize to lower, remove punctuation, then split on whitespace.

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

std::vector<std::string> tokenize(std::string s) {
    std::transform(s.begin(), s.end(), s.begin(),
                   [](unsigned char c){ return std::tolower(c); });
    s.erase(std::remove_if(s.begin(), s.end(),
               [](unsigned char c){ return std::ispunct(c); }), s.end());
    std::istringstream iss(s);
    std::vector<std::string> words;
    std::string w;
    while (iss >> w) words.push_back(w);
    return words;
}

For exact phrase detection use a compiled regular expression so you match whole words (not substrings). Precompile the regex and reuse it for performance.

#include <regex>

std::regex phrase(R"(\bhow\s+are\s+you\b)", std::regex_constants::icase);
if (std::regex_search(input, phrase)) reply = "I'm fine";

If matching should be forgiving (typos, variants) use an edit-distance check (Levenshtein) or n-gram similarity with a small threshold. Also note practical issues: std::regex and unordered containers require modern compilers (older Turbo-era compilers lack them), UTF-8 needs a different tokenization approach, and always cast to unsigned char when calling C character functions. For persistence and scaling, keep stored triggers normalized so lookups and later learning remain robust — a point mentioned earlier by .

Recommended Answers

All 2 Replies

Well how advanced do you want it to be. When somebody types,

What is the reason for OO design?

do you want the program to give an opinionated, zealoted reply?

Or do you just want to search for "hello", "how are you" and if there's a match reply then?

A generic way to do this would be to have a file with questions and responses. You can then load the entire file when the program starts into a map<string,string>. Then, you can then search the map for the question and if a response is present, return it. If it's not present, you can ask the user for the appropriate response and then add it to the file for the next time.

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.