Thread: counting strings

  1. #16
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    You can change the for condition to:

    Code:
    for (int i=0; i<s.size()-2;i++)
    That'll stop it from running out of bounds.

    find("and") will (if I understand correctly) keep finding the same "and" over and over again. You can pass it a place to start as the second parameter.

    Code:
        while(getline(infile, s))
        {
            size_t strpos = 0;
            strpos = s.find("and"); // search from the start
            
            while (strpos != string::npos)
            {
               count++;
               strpos = s.find("and", strpos+1);           // search the rest of the string
            }        
        }
    find() would return 0, or false, if it found an "and" at the start of the string, so I'm surprised that that was working for you.

  2. #17
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    Works very well thank you. That way is much easier to read strings.

    It work perfectly every time I made sure there were no \n in the sentence so the string would read the whole page. How would someone search a txt file with several \n\n, like how a essay is structured or a new article.

  3. #18
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by jocdrew21 View Post
    Works very well thank you. That way is much easier to read strings.

    It work perfectly every time I made sure there were no \n in the sentence so the string would read the whole page. How would someone search a txt file with several \n\n, like how a essay is structured or a new article.
    If what you have doesn't handle files with \n in it, you didn't do it right. All the things people have posted to help you with handle \n just fine, so use one of those techniques, such as putting the getline as the control of a while loop, to handle your file.

  4. #19
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    I don't really understand the problems that '\n' is causing you.
    getline will stop reading when it gets to a '\n'. It won't read the '\n' into the string, so if you output all the strings you'd read you'd lose newlines.

    I'm not convinced that it's a good idea to work around getline stopping on '\n'. It's not a bad thing to process things in small chunks -- is there a particular reason you want to load the whole page?

    You can do so by concatenating everything from getline into a mega-string:
    Code:
    string temp_str;
        string s = "";
        while(getline(infile, temp_str))
            s += temp_str + "\n";
            
        size_t strpos = 0;
        strpos = s.find("and");
        
        while (strpos != string::npos)
        {
           count++;
           strpos = s.find("and", strpos+1);           
        }
    I also added the newlines back in, which you can remove if you like. I don't think that this is a good thing to do though - granted, if we're talking about a page of text it's hardly going to cause memory consumption problems -- it's more that I don't understand why paragraph by paragraph is no good.

  5. #20
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    I wanted to learn more about how to do this because a few weeks ago I had an assignment and I used eof() in the while statement and got some negative feedback on its implementation. Upon looking for new ways I found the find() function.

    I am doing some fun projects before data structures start. With every fun program I write, it seems to takes me to a whole new area of thought. Paragraph by paragraph I suppose is not a bad idea, I just wanted a mega string to search a news article and count certain words from a txt file. Thank you very much @smokeyangel for the awesome example.

  6. #21
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Warning: not tested code.
    You can use stream iterators. Something like:

    Code:
    #include <fstream>
    #include <iterator>
    #include <string>
    #include <algorithm>
    
    int main()
    {
    	std::ifstream myfile("myfile.txt");
    	std::istream_iterator<std::string> begin(myfile), end;
    	int count = 0;
    
    	for (;;)
    	{
    		auto find_it = std::find(begin, end, "and");
    		if (find_it == end)
    			break;
    		count++;
    		begin = find_it++;
    	}
    }
    This may not 100% stable, but it kind of does what you wanted it to do: it reads and searches the entire file instead of processing it in chunks.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #22
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    Quote Originally Posted by Elysia View Post
    Warning: not tested code.
    You can use stream iterators. Something like:

    Code:
    #include <fstream>
    #include <iterator>
    #include <string>
    #include <algorithm>
    
    int main()
    {
        std::ifstream myfile("myfile.txt");
        std::istream_iterator<std::string> begin(myfile), end;
        int count = 0;
    
        for (;;)
        {
            auto find_it = std::find(begin, end, "and");
            if (find_it == end)
                break;
            count++;
            begin = find_it++;
        }
    }
    This may not 100% stable, but it kind of does what you wanted it to do: it reads and searches the entire file instead of processing it in chunks.
    This is a very clear way to do it. I will certainly test this and play around with this when I get home from work. This is the first time I seen <iterator>...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 07-20-2013, 08:34 AM
  2. Strings and file word counting Program
    By hotshotennis in forum C Programming
    Replies: 3
    Last Post: 05-23-2012, 01:28 PM
  3. Counting number of strings in an array
    By Hawkin in forum C Programming
    Replies: 4
    Last Post: 06-21-2010, 11:25 AM
  4. Counting a character with strings
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 03-06-2002, 09:07 AM
  5. Counting Strings
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 10-29-2001, 04:31 PM