Thread: help counting characters

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    4

    help counting characters

    Hi. I am having a problem. What I am trying to do is input a text file. From the file I want to find out how many words are on there and then I want to know how many characters are in the text file for each letter. For example :the dog.

    That has 1 t, 1 h, 1 e, 1 d, 1 o, 1 g, 1 .

    Then i need to output the text from the file to the screen and then output how many words and each of the letters in alphabetical order.

    How can i do that? So far all I have been able to do is count how many words but I cannot find a way of how to count the characters or anything. Can anyone help?

    This is my code:
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    
    int main()
    {
    string word;
    int count=0;
    ifstream fin;
    
    fin.open("input.txt");
    
    if (fin.fail())
    {
    cout<<"Couldn't read file. \n";
    cin.get();
    return 0;
    }
    
    while(!fin.eof())
    {
    fin>>word;
    cout<<word<<" ";
    ++count;
    }
    
    
    cout<<"\nThere are: "<<count<<" words";
    fin.close();
    
    cout<<"\nPress enter to exit";
    cin.get();
    return 0;
    
    }

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Read character-by-character, store the character in a map paired with an int that contains its count. Then try to lookup the character you read and, if found, increase its count, and so on.
    For words, just read word-by-word (what you're doing now) and increase count.

    Refer to site tutorials for help on maps and such.
    Also, fin.eof() is bad for loop condition. See FAQ.
    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.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    4
    is there any other way of doing it without using map? I'll say it now, I am trying to do this for homework and I know writting the code for me is not right. But I need help in direction. is there anyway of doing it just using loops and commands from strings, arrays, and file i/o?
    Last edited by dom89; 12-08-2007 at 05:53 PM.

  4. #4
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    hint: try to index an array by character.
    Code:
    int count[256] = { 0 };
    ...
    count['a']++;
    ...

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    4
    thanks cyberfish. With the array I was able to count how many of each letter there is. Now how can I limit the output to only output the ones that have a value greater than 1? If the letter is not in the text, I would like to not print it out. How would I do that also with the characters? including the difference when they are CAPS or lower case?

    here is my new code after i added the arrray

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    
    int main()
    {
    	string word;
    	int letter[126]={0};	
    	int count=0, a;
    	char ch[126];
    	ifstream fin;
    
    	fin.open("input.txt");
    		
    	if (fin.fail())
    	{
    		cout<<"Couldn't read file. \n";
    		cin.get();
    		return 0;
    	}
    	
    	while(!fin.eof())
    	{
    		fin>>word;
    		cout<<word<<" ";
    		++count;
    		a=word.length();
    		
    		for(int i=0; i<a;++i)
    		{
    			ch[i]=word[i];
    			++letter[ch[i]];
    	
    		}	
    		}
    	
    	cout<<"\nThere are: "<<count<<" words";
    	for(int i=0; i<126;++i)
    		{
    			
    		cout<<" "<<letter[i]<<endl;
    		}
    	
    	fin.close();
    	
    	cout<<"\nPress enter to exit";
    	cin.get();
    	return 0;
    	
    }
    Last edited by dom89; 12-08-2007 at 06:59 PM.

  6. #6
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    Code:
    for (char i = 'a'; i <= 'z'; ++i) {
         if (count[i] != 0) {
              ...
         }
    }
    use another loop for uppercase letters.
    Last edited by cyberfish; 12-08-2007 at 10:26 PM.

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    4
    thanks cyberfish. that helped me out a lot. Now since I want to include such things as periods, all i have to do is set the initial value to "!" and the last value to "z". Am i correct?

  8. #8
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    yes, that would work.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Counting the characters from each word from a text file
    By flipguy_ph in forum C Programming
    Replies: 6
    Last Post: 04-27-2009, 05:56 PM
  2. Replies: 10
    Last Post: 07-10-2008, 03:45 PM
  3. Help with hw problem: counting characters in a string
    By pinkfloyd4ever in forum C++ Programming
    Replies: 11
    Last Post: 11-04-2007, 11:18 PM
  4. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  5. Counting characters
    By tay_highfield in forum C Programming
    Replies: 3
    Last Post: 01-29-2003, 12:54 PM