Hi. I need to create a function that prints the frequency of all words from two given texts. I must do this using mainly POINTERS. I already have the first part of the program which prints the common words from the two texts, now all I need is to display the frequency of each word. Any tips or suggestions are welcome.
PS: Disregard the actual texts, they are in Romanian.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
int main()
{
char text_A[512] = "Ana are mere. Andrei i le-a mancat pe toate.";
char text_B[512] = "Ana nu mai are mere din cauza ca Andrei i le-a mancat.";
char* token = strtok(text_A, " ,.!?");
while (token)
{
if (std::strstr(text_B, token))
{
std::cout << token << std::endl;
}
token = strtok(nullptr, " ,.!?");
}
}
This one counts the words in text_A to not change your loop, but you may consider changing the loop to just iterate over 512 characters so that you can compare and tokenize both A and B at the same time.
#include <iostream>
#include <cstring>
#include <map>
int main()
{
char text_A[512] = "Ana are mere. Andrei i le-a mancat pe toate.";
char text_B[512] = "Ana nu mai are mere din cauza ca Andrei i le-a mancat.";
std::map<std::string, int> freq;
char* token = strtok(text_A, " ,.!?");
while (token)
{
if (std::strstr(text_B, token))
{
std::cout << token << std::endl;
}
freq[token] += 1;
token = strtok(nullptr, " ,.!?");
}
std::cout << "\n\n";
for (auto& item : freq)
std::cout << item.first << ": " << item.second << std::endl;
}
Well, this is a homework about Pointers. So I'm guessing we have to focus on pointers mostly. The only things we studied by now are Stack, Queue and Pointers. We are also allowed to use strtok and other things that don't require extra libraries to be imported. This is only the 4th week of the course.
what are all the things you need to do? Is it just
...to create a function that prints the frequency of all words from two given texts... using mainly POINTERS
? Is finding common words an actual requirement, or did you do that for fun?
map, ordered or unordered, is a very common way to determine frequency. You'll find this same strategy in all sorts of languages -- Ruby, Python, etc., where sometimes it's called a "hash", or a "dictionary". You can probably change your loop to use pointers, but in the end the storage will likely be in a map.
Just found a strtok_r which allows remembering a position (using pointers!) for a particular string. The regular strtok is static and so doesnt support remembering two string positions.
If you don't need the common strings, this will do the total frequency job of both texts:
Ok so the assignment asks to print the common words of two texts using pointers, and then to display the frequency of words using pointers. Probably ok if in two separate programs. The thing is I am sure we can't use map yet, because we barely studied the basics. In the course objectives map/hash is at the bottom of the list. So I think we should use POINTERS ONLY for this. Thank you for your solutions anyway @icy1 and sorry for wasting your time.
Yes, I am allowed. So how exactly would I iterate through words using pointers? I know how to iterate through chars, but how do I save an entire word? Do I need something like two pointers?
Here's an example using std::strtok() to parse out whole words.
http://en.cppreference.com/w/cpp/string/byte/strtok
1 2 3 4 5 6 7 8 9 10 11 12 13
#include <cstring>
#include <iostream>
int main()
{
char input[100] = "A bird came down the walk";
char *token = std::strtok(input, " ");
while (token != NULL)
{
std::cout << token << '\n';
token = std::strtok(NULL, " ");
}
}
A
bird
came
down
the
walk
Easy to adapt for storing the tokenized words in an array. Only one pointer needed.
There is no C++ standard strtok() equivalent for dealing with a C++ std::string. There are 3rd party solutions. Boost has one, as well as numerous "hand-rolled" solutions.
http://oopweb.com/CPP/Documents/CPPHOWTO/Volume/C++Programming-HOWTO-7.html
POSIX platform only, not part of the C standard. C11 has strtok_s().
http://en.cppreference.com/w/c/string/byte/strtok
The strtok_s function differs from the POSIX strtok_r function by guarding against storing outside of the string being tokenized, and by checking runtime constraints.