Need help with comparing word

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

void main()
{
	string text, temp[200], search;
	int count=0;

	ifstream openf;
	openf.open("mytext.txt");

	cin>>search;

	for(int j = 0; j < 200; j++)
	{	openf>>temp[j];
		if (strcmp (search.c_str(),temp[j].c_str()) == 0)
		{
		count+=1;
		}
	}
	
	for (int a=0;a<200;a++)
	{
		if (strcmp (search.c_str(), temp[a].c_str()) == 0)
			{
				for (int b=0;b<count;b++)
				{
					cout<<temp[a++]<<" ";
				}
				cout<<endl;
			}
	}
	cout<<count<<endl;
}


The text file has 5 of the word 'the' and it comes out 5 but i cannot seem to extract the 2 words before the word 'the' and 5 words after the word 'the'. other then that how can i make this code better, maybe using stringstream, but i do not know how to. I'm still new to C++, lead me in the right direction please.
Last edited on
I do not understand what you want to achieve.
Please explain what you want to do, and not how you tried to do it.

Small observation:

1
2
3
4
5
if (strcmp (search.c_str(),temp[j].c_str()) == 0)

// can be simplified as

if (search == temp)


This is because comparison operators are overloaded for std::string:
http://www.cplusplus.com/reference/string/string/operators/

However, the string named search should either:
a) be renamed
b) or you should stop using namespace std;

This is because:
http://www.parashift.com/c++-faq/using-namespace-std.html
http://www.cplusplus.com/reference/algorithm/search/
I am trying to compare input word from user to a sentence in a text file and count how many of the word appear in the sentence then try to print out the sentence which has the word that was compared , tell me how to do or head me to the right source? thank you
A possible algorithm could be as follows:

read user word

while (text file has sentences)
{
    if (sentence has word)
        count how many times word appears

    print sentence
}

print total count of words found


For this you will use std::getline() and std::string::find():
http://www.cplusplus.com/reference/string/string/getline/
http://www.cplusplus.com/reference/string/string/find/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
std::cin >> user_word;

std::string line_from_file;
std::size_t total_count = 0;

while (std::getline(input_file, line_from_file))
{
    if (line_from_file.find(user_word) != std::string::npos)
        std::cout << line_from_file << '\n';

    std::size_t p=0;

    while ((p = line_from_file.find(user_word, p)) != std::string::npos)
    {
        ++total_count;
        p += user_word.length();
    }
}

std::cout << user_word << " appears " << total_count << " times." << std::endl;


The above code was not tested, so happy hunting for bugs!
thank you, from your algorithm i have the general idea, but from the above code i noticed that if i search a word for example the word 'an' ..it will count the word 'an' in any word that has an 'an' like 'android', 'and', 'answer'..how can i prevent that??
You could add a space to the word when you search for it.

// input text
"an android and his dog an"

// user word
"an"

// instead search for
"an" + ' ' == "an "


p = line_from_file.find(user_word + ' ', p)

After that you must remember the special case when the word appears last in the line and is not followed by a space. (You could add a space to the input text as well.)

Edit: also if your input text contains symbols, such as ,.?!;: and tabs, you should first make a pass and replace all of them with simple space.
Last edited on
Topic archived. No new replies allowed.