The letter e is the most frequently used letter in English prose, and the letter
ID: 3641135 • Letter: T
Question
The letter e is the most frequently used letter in English prose, and the letter z is the least frequently used. A friend of yours doing a sociology experiment believes that this may no necessarily be true of the writings of first year college students. To test his theory, he asks you to write a program that will take a text file and print, for each letter of the English alphabet, the number of times the letter appears in the file.Hint: Use an integer array of size 128, and use the ASCII values of letters to index into the array to store and retrieve counts for the letters. The text file can be text of anything don't need code for text file only code for this problem and a screen shot of actual text file that would get opened.
Explanation / Answer
#include <iostream>
#include <fstream>
using namespace std;
//PROTOTYPES
void countCharacters(const char *word, int *charStorage);
void countEnglishLetters(const int *charStorage, int *EnglishLetterStorage);
int main()
{
//LOCAL DECLARATIONS
int charStorage[128];
int EnglishLetterStorage[26];
char word[20];
char fileName[20];
fstream fin;
//Get file name
cout << "Enter file name: ";
cin >> fileName;
//Open file
fin.open(fileName, ios::in);
if (!fin)
{
cerr << "Error: Cannot open file " << fileName << endl;
exit(1);
}
//Initialize 2 arrays
for (int i = 0; i < 128; i++)
charStorage[i] = 0;
for (int i = 0; i < 26; i++)
EnglishLetterStorage[i] = 0;
//Count all characters in the file
while (fin >> word)
countCharacters(word, charStorage);
//Extract only English characters (merge upper case and lower case letters)
countEnglishLetters(charStorage, EnglishLetterStorage);
//Display result
for (int i = 0; i < 26; i++)
cout << (char)(i + 'a') << ": "
<< EnglishLetterStorage[i] << endl;
//Close file
fin.close();
//Pause the program before exiting
cin.sync();
cin.get();
return 0;
}
//---------------------------------------------------------
// FUNCTION DEFINITIONS
//---------------------------------------------------------
void countCharacters(const char *word, int *charStorage)
{
const char *ptr = word;
while (*ptr)
charStorage[*ptr++]++;
}
//---------------------------------------------------------
void countEnglishLetters(const int *charStorage, int *EnglishLetterStorage)
{
for (int i = 'A'; i <= 'Z'; i++)
EnglishLetterStorage[i - 'A'] += charStorage[i];
for (int i = 'a'; i <= 'z'; i++)
EnglishLetterStorage[i - 'a'] += charStorage[i];
}
//---------------------------------------------------------
//Sample output of the Declaration of Independence: (http://www.ushistory.org/declaration/document/)
Enter file name: doi.txt
a: 562
b: 105
c: 213
d: 272
e: 950
f: 191
g: 154
h: 401
i: 511
j: 35
k: 22
l: 286
m: 183
n: 558
o: 589
p: 153
q: 6
r: 505
s: 534
t: 698
u: 224
v: 78
w: 121
x: 10
y: 97
z: 4
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.