-
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;
}
-
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.
-
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?
-
hint: try to index an array by character.
Code:
int count[256] = { 0 };
...
count['a']++;
...
-
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;
}
-
Code:
for (char i = 'a'; i <= 'z'; ++i) {
if (count[i] != 0) {
...
}
}
use another loop for uppercase letters.
-
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?
-