Thread: counting strings

  1. #1
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499

    counting strings

    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    #include <array>
    
    #define MYFILE "/Users/peterjocham/Desktop/textfile.txt"
    
    using namespace std;
    
    int main()
    {
        ifstream infile(MYFILE,ios::in);
        
        string s;
        int count=0;
        
        getline(infile, s);
        
        for (int i=0; i<sizeof(s);i++)
        {
            if(s[i]=='a'&&'n'&&'d')
            {
                count++;
            }
        }
        
        cout<<"and appeared: "<<count<<" times"<<endl;
        
        
        return 0;
    }
    I am trying to count how many time the word 'and' occurs but I am getting wrong numbers.

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
            if(s[i]=='a'&&'n'&&'d')
    that won't do what you want, it will be true for each 'a'
    try
    Code:
     if(s[i]=='a' && s[i+1]='n' && s[i+2]=='d')
    Beware of out of bounds accesss

    Kurt

  3. #3
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    Code:
     for (int i=0; i<sizeof(s);i++)
            {
                if(s[i]=='a' && s[i+1]=='n' && s[i+2]=='d')
                {
                    count++;
                }
            }
    @Kurt, this does work. I am thinking I might need a while statement. It is reading only one line.
    Last edited by jocdrew21; 11-23-2013 at 04:55 AM.

  4. #4
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Yes, you could put the getline call as the condition for a while loop.
    The other mistake is that sizeof() wont return the length of the string
    Kurt

  5. #5
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    Code:
     for (int i=0; i<s.size();i++)
            {
                if(s[i]=='a' && s[i+1]=='n' && s[i+2]=='d')
                {
                    count++;
                }
            }
    Got it working... The file had a '/n' and therefore it stopped read it. How would I ignore this? Lets say I want to search a whole book or paragraph for certain words, count them etc.

  6. #6
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by jocdrew21 View Post
    Lets say I want to search a whole book or paragraph for certain words, count them etc.
    as I said before try
    Code:
    while ( getline(infile, s) ) {
        ... // your for loop
    }
    And you are still accessing the string out of bounds

    Kurt

  7. #7
    Registered User
    Join Date
    Mar 2010
    Posts
    582
    You should use size()-2 in that for loop. So long as there's a newline at the end it won't crash, because if s[i] == '\n' then s[i] != 'a', so the other two conditions aren't evaluated. At the end of a file though there might not be a newline.

    getline() will stop when it reads a newline, so you need to put another loop around it all. You can put getline in a while loop like so:

    while(getline(infile, s))
    {
    The while loop will terminate when you get to the end of the file. By scary C++ black magic which I've tried and failed to understand.

  8. #8
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    I got it to work. Now I am messing around with capitalizing "and" to "AND" and outputting it back to the txt file.

    Thank you for the help...

  9. #9
    Registered User
    Join Date
    Mar 2010
    Posts
    582
    You could have also used s.find("and") -- string::find - C++ Reference

    Good luck with that! It'd definitely be easiest to write a new file copying over each line from the old one, with the string modified when there are "and"s. It should be possible to do it without doing that by tracking and manipulating the get and put pointers. I've never wanted to avoid a file copy and replace badly enough to try it!

  10. #10
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    Code:
    while(getline(infile, s))
        {
    
            for (int i=0; i<s.size();i++)
            {
                if(s[i]=='a' && s[i+1]=='n' && s[i+2]=='d')
                {
                    s[i]='A';
                    s[i+1]='N';
                    s[i+2]='D';
                    count++;
                }
            }
            
        }
    This works and gives me the right amount of "and" that I am looking for. If I put a break after the loop then one paragraph "and" are capitalized.

    I tried:
    Code:
    while(getline(infile, s))
        {
    
            if (s.find("and")) {
                count++;
            }
            
        }
    After it was mentioned. I seen this on a google search too but it didn't work then either. I am using it wrong I know that. How do you use it correctly? This looks very nice and simple.

    I have not yet finished messing with this program, I found lambda's and been reading about that for a few hours. Really cool.....
    Last edited by jocdrew21; 11-23-2013 at 07:54 AM.

  11. #11
    Registered User
    Join Date
    Mar 2008
    Posts
    10
    Hi, in this case the easy way is to use the operator >>, It returns the istream object that evaluates to false/true if you use it like a boolean.

    Code:
    ...
    while(infile >> s){
            count += s == "and";
    }
    ...
    Regards.

  12. #12
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499

    Not working:

    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    
    #define MYFILE "/Users/peterjocham/Desktop/textfile.txt"
    
    using namespace std;
    
    int main()
    {
        ofstream outfile(MYFILE,ios::app);
        
        ifstream infile(MYFILE,ios::in);
        
        string s;
        int count=0;
        
        while(infile >> s)
        {
                count += s == "and";
        }
        
        cout<<"and appeared: "<<count<<" times"<<endl;
        cout<<s;
        
        
        return 0;
    }
    Tried it your way and it is not working... I am getting 9 and's and I have 16 in the file.

  13. #13
    Registered User
    Join Date
    Mar 2008
    Posts
    10
    Ok, I think you are looking for the string "and" matching over the text, and not the words "and" that there are in, that's different. So try to use string::find.

  14. #14
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    Code:
    while(infile >> s)
        {
           if(s.find("and")==false)
           {
               count++;
           }
            
        }
    This is working but it is only reading the first paragraph.

    This code read the whole page and searched for "and" :
    Code:
    while(getline(infile, s))
        {
     
            for (int i=0; i<s.size();i++)
            {
                if(s[i]=='a' && s[i+1]=='n' && s[i+2]=='d')
                {
                    s[i]='A';
                    s[i+1]='N';
                    s[i+2]='D';
                    count++;
                }
            }
             
        }
    However I was accessing the string out of bounds.


  15. #15
    Registered User
    Join Date
    May 2010
    Posts
    4,553
    You need to read the documentation for the string.find() function. It doesn't return a bool, it returns a size_t, and it doesn't return zero if it doesn't find the string it returns string::npos. Also when it finds the string it returns the position in the string where the found string starts.

    If you're using the extraction operator instead of getline() you don't actually need the string.find() function just use the comparison operator.

    Code:
    if(s == "and")
     ...
    Jim

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