简体   繁体   中英

find repeated words number in c++

is there anyway to find out how many times a word repeated in a text .

the text is in character arrays (char[])

text = this is a book,and this book is about book.

word = book

result = 3

Because this is clearly homework and not tagged as such, I'll give you a solution you clearly can't submit as your assignment because your teacher will know you got it on the internet.

There were no requirements such as ignoring punctuation, so I've allowed myself to write a version that only works for clearly separated words and thus inserted spaces in your sample text string.

#include <algorithm>
#include <iostream>
#include <iterator>
#include <sstream>
#include <string>

    // Count clearly separated occurrences of `word` in `text`.
std::size_t count ( const std::string& text, const std::string& word )
{
    std::istringstream input(text);
    return (std::count(std::istream_iterator<std::string>(input),
        std::istream_iterator<std::string>(), word));
}

int main ( int, char ** )
{
    const char text[] = "this is a book , and this book is about book .";
    const char word[] = "book";
    std::cout << count(text, word) << std::endl;
}

Output:

3

您可能要使用std::string来实现这一点, 是一个示例供您开始。

The simplest way would be to loop through the string, counting the number of times that you find the word that you're looking for. I'm sure that you could use a function in <algorithm> to do it fairly easily, but if you have to ask whether it's possible to do this in C++, I wouldn't think that you're advanced enough to try using the algorithm library, and doing it yourself would be more instructional anyway.

I would suggest using std::string though if you're allowed to (since this question does sound like homework, which could carry additional restrictions). Using std::string is easier and less error-prone than char arrays. It can be done with both though.

It is possible.

You have an array of characters. Try to do the search on a piece of paper, character by character:

First character is a T. This is not ab, so it can't be the first character of "book" Second character is ah, so again, it is not b...

[...]

The next character is a b... Oah, this could be it. Is the next character ao? YES!!! And then next another o???... etc. etc..

When you can do it this way, you will be able to use C++ to do it.

Remember that you can access the n-th character in an array by using the [] operator:

char c = array[5] ; // c is the 6th character in the array

Now, going toward the C++ way would be, at first, to use a std::string instead of an array of chars, and use the strings methods. Google for std::string methods , and I guess you should find somes that you could use... So you should manage to write some code that will iterate each character until the end

I guess this should be more than enough.

The point of your homework (because everyone here knows this is a homework question) is more about searching for the solution than finding it: This is not rote learning.

I doubt anyone on Stack Overflow remembers the solution to this classical problem. But I guess most will know how to find one solution. , so get your compiler and try again... ,因此请获取编译器,然后重试...

PS: Of course, if you know little or nothing of C++, then you're screwed, and you could start by Googling some C++ Tutorials .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM