:sad: There is a programming question. This is searching program. there is a string array. In this string array ther is a sentence. I get a word from the user . When the user's word is match with the sentence that is in the string , a message will appear ( found!!) and the number of count else (Not found!!).
My coding:
main()
{
int count ;
char st1[80],st2[80];
char st[80]={ ncc education examination ncc ncc ncc education};
cout<< "Enter a word to be search";
cin>> st[];
int i=0;
{

while(st!='/0')
{ st3=st1;
st3++;
while((isspace(st)))
if( stcmp(st1,st3));
count++;
{cout<< "found";}
else {cout<<"Not fjound!!";}
}
st1++;
}
}
Please test this program is wrong or true .If in this program have a little wrong please correct it

Dani AI

Generated

asked for a program that searches a sentence for a user-supplied word, reports "Found!!" and prints the count, otherwise "Not found!!". The original post had fragmented C-style arrays and control flow issues; and were right to ask for a complete, clean example. Below is a small, compilable C++ program that performs whole-word, case-insensitive matching and counts occurrences reliably.

#include <iostream>
#include <string>
#include <regex>
#include <iterator>

std::string escape_regex(const std::string& s) {
    const std::string meta = "\\^$.|?*+()[]{}";
    std::string r; r.reserve(s.size()*2);
    for (char c : s) {
        if (meta.find(c) != std::string::npos) r.push_back('\\');
        r.push_back(c);
    }
    return r;
}

int main() {
    std::string sentence;
    std::cout << "Enter a sentence: ";
    std::getline(std::cin, sentence);

    std::cout << "Enter word to search: ";
    std::string word;
    if (!(std::cin >> word)) return 0;

    std::string pattern = "\\b" + escape_regex(word) + "\\b";
    std::regex re(pattern, std::regex_constants::icase);
    auto begin = std::sregex_iterator(sentence.begin(), sentence.end(), re);
    auto end = std::sregex_iterator();
    auto count = std::distance(begin, end);

    if (count > 0)
        std::cout << "Found!! count = " << count << '\n';
    else
        std::cout << "Not found!!\n";

    return 0;
}

Notes and troubleshooting: compile with a modern compiler (C++11 or later). The code uses word boundaries so it matches whole words, not substrings; punctuation is handled by the regex boundaries. If std::regex is unavailable or slow on your toolchain, tokenize the sentence (split on non-alpha characters), normalize case, and compare tokens. Also be careful when mixing getline and operator>>—read the full line first (as shown) or flush the leftover newline.

Recommended Answers

All 2 Replies

1. Use complete sentences to describe what you are trying to accomplish so someone can understand what you are asking. So they do not have to lower themselves to a third grade level of reading to know what you are trying to ask.

2. Use code tags when posting code.

3. main returns an int it should be int main.

Please provide full code and write down exactly what is what they want from you at school

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.