Just create an array of 256 integers, initialize each one to 0.
Then character by character go through the file. Index into the array using the ASCII value of the character and increment the count. Repeat until EOF.
At the end of the file, just print out the counts for the alphabet characters (note: you will need to add the lower case and upper case values together)
2007-12-01 16:16:49
·
answer #1
·
answered by mdigitale 7
·
2⤊
0⤋
Alice Brenna Charlotte Denise Eva Fiona Gianna Helena Isabelle Jessica Katherine Laura Melody Naomi Olivia Penelope Quinn Rachel Samantha Tallulah Uma Victoria Wren Xia Yasmin Zara
2016-10-25 07:12:35
·
answer #2
·
answered by ? 4
·
0⤊
0⤋
Like some said...make an array if int, 0-256
read in the file and make the ascii value f the char be the index to the array
Add 1 to the element each time you go there.
array[ "ascii value"] +=1;//first 0 then adds 1
if you read in 'a' you would go to index 97, capital A would be 65.
When you are done you will know how many of each letter you have, small and capital.
2007-12-01 16:34:49
·
answer #3
·
answered by ? 6
·
0⤊
0⤋
If you still stuck, here's some sample code.
...
char c;
int tab[52];
ifstream inFile("text.in");
while (inFile >> c) {
if (isupper(c)) tab[c-'A'+26]++;
else if (islower(c)) tab[c-'a']++;
}
for (int i = 0; i < 26; i++) cout << "The letter " << (char)i+'a' << " appeared " << tab[i] << " times.\n";
for (int i =26; i < 52; i++) cout << "The letter " << (char)i+'A' << " appeared " << tab[i] << " times.\n";
...
Pretty straightforward implementation, but should show you the idea behind the algorithm.
2007-12-02 01:44:52
·
answer #4
·
answered by Senka T 2
·
0⤊
0⤋
use a coding system...in this case you can use numbers 1-26 starting with A being 1.
2007-12-01 16:13:06
·
answer #5
·
answered by leguticia 2
·
0⤊
1⤋
assuming all lower-case, you just need an array of 27 ints, and do something like this: (assuming 'file' contains the file's contents)
int array[27];
//foreach byte in file
for(i = 0; i < charsInFile; i++) {
array[ ((int) file[i] - 97) ]++;
}
then loop through 'array' at the end to print out the counts, converting back to ascii:
//for reach int in 'array'
foreach(array)
{
printf("%c=%d\n", (char) (i+97), array[i] );
}
2007-12-01 16:34:00
·
answer #6
·
answered by fixedinseattle 4
·
0⤊
2⤋
awkward.
2007-12-01 16:12:05
·
answer #7
·
answered by Twizzler3 3
·
0⤊
2⤋