954,483 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Searching problem

Hey guys, I am using the code below ot search for words in a text file and count them. I now would like to do something a little more advanced for example if a text file contains
"hello my name is kim" I want to be able to increment a variable everytime the words hello and my appear next to eachother, is there an easy way to do this?

Thanks.

char array [200];
ifstream file;

      while (file >> array)
	   {
			if(strstr(array,"test") != 0)
                        test++;
           }
Jon182
Junior Poster in Training
91 posts since Jul 2005
Reputation Points: 10
Solved Threads: 0
 

Can you not use strstr to look for "hello my"? Or are you looking for any combination such as "my hello", hellomy", and so forth? In that case, the easiest way would be to use a regular expression, which C doesn't support as a standard library. You would need to write your own manually or use a third-party library. Alternatively, you can just hardcode a test for every possible combination. Your choice.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

Hi thanks for your quick reply I would hardcode each, but i'm not sure what you mean by a regular expression, what is that?

Thanks for your reply.

Jon182
Junior Poster in Training
91 posts since Jul 2005
Reputation Points: 10
Solved Threads: 0
 

A regular expression is a way to search for patterns rather than exact matches.

http://en.wikipedia.org/wiki/Regular_expression

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

Thanks, sorry i'm not very good at c++ how would I go about coding that? Could i use my array and the code I have posted above?

Jon182
Junior Poster in Training
91 posts since Jul 2005
Reputation Points: 10
Solved Threads: 0
 

>Thanks, sorry i'm not very good at c++
This has nothing to do with C++ since you've shown that you can do a simple string search. At this point it's a problem solving issue. I personally don't think it's terribly difficult to do this:

if ( strstr ( array, "hello my" ) != NULL )
  ++test;
if ( strstr ( array, "hellomy" ) != NULL )
  ++test;
etc...

And since you're probably new to programming, it's unlikely that you'll have an easy time working with regular expressions.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

Thanks again for you help, I am just wondering how come its !=NULL rather than !=0?

The code also doesn't seem to be working for me is it because I am reading the file in one word at a time?

Jon182
Junior Poster in Training
91 posts since Jul 2005
Reputation Points: 10
Solved Threads: 0
 

>I am just wondering how come its !=NULL rather than !=0?
Both are the same. NULL simply clarifies your intentions of using 0 in a pointer context. But if you use it, be sure to include a header that defines it. In C++ you'll probably want for NULL unless you use other C libraries.

>it because I am reading the file in one word at a time?
Yes. Use getline to read an entire line and it should work better.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

> This has nothing to do with C++ since you've shown that you can do a simple string search. At this point it's a problem solving issue. I personally don't think it's terribly difficult to do this:

if ( strstr ( array, "hello my" ) != NULL )
  ++test;
if ( strstr ( array, "hellomy" ) != NULL )
  ++test;
etc...

And since you're probably new to programming, it's unlikely that you'll have an easy time working with regular expressions.


That is a "C" way of doing things, which is fine :)

Here is one method which is a bit more C++... (Yes OK, it is actually a bit more difficult than Narue's method)

int count(0);
    std::string::const_iterator iter(LineIn.begin());

    while(iter != LineIn.end() )
    {
        iter = std::search(LineIn.begin(), LineIn.end(), 
                           match.begin(), match.end() );
        if (iter != LineIn.end() )
        {
            ++count;
            ++iter;
            std::string s(iter, LineIn.end());
            LineIn=s;
        }
        
    }

LineIn is a std::string - using getline() from a file
match is a std::string - the string to match, eg, "hello my"

This method will not ignore whitespace, and will find multiple matches per string.
Although be careful to make sure that the strings are an exact match, ie, "Hello" != "hello" != "HELLO" != "HeLlO" .. etc.

the easy way around the mismatched strings is to change LineIn and match both 'tolower' (or 'toupper' - whichever floats your boat)

Bench
Posting Pro
577 posts since Feb 2006
Reputation Points: 307
Solved Threads: 63
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You