开发者

find repeated words number in c++

开发者 https://www.devze.com 2023-01-25 04:17 出处:网络
is there anyway to find out how many times a word repeated in a text . the text is in character arrays (char[])

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

开发者_如何学Python


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


You might want to implement this using std::string and here is a sample for you to start from.


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 a b, so it can't be the first character of "book" Second character is a h, so again, it is not b...

[...]

The next character is a b... Oah, this could be it. Is the next character a o? 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. You need to learn the "how to find" mindset, so get your compiler and try again...

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

0

精彩评论

暂无评论...
验证码 换一张
取 消