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.
Question
The letter e is the most frequently used letter in English prose, and the letter z is the least frequently used. So a friend of yours doing a sociology experiment believes that this may not necessarily be true of the writings of first-year college students. To test his theory, he asks you to create a program that will take a text file and print it. 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.
Summary
In this we have given a text file for which each character is going to be scanned. The file is to be read character by character. And then the frequency of each character that has been read is stored in an array. i.e. At the character of the ascii value index. Once the frequency of each character is stored in the array it is going to be printed on the screen as an output.
Explanation
Here, we have taken a file named ‘data.txt’ and have written a text in it. After that, we have used ifStream object to open the file in reading mode. So then incrementing the array index by one each character is read with its corresponding ASCII value. And then Finally after this process reading the file character by character. The character and its frequency are going to be printed on the screen as an output. Also, the loop is included so that the loop is going to iterate to find which character is most frequent and which character is least frequent. And print that on the screen as an output.
Code
#include <iostream> using namespace std; #include <fstream> #include <iomanip> int main() { /* array to store frequencies of each character in the story */ int arr[123]; /* initially, the frequency is zero for every index */ for(int i=0;i<123;i++) arr[i]=0; /* to read the input file */ ifstream infile; infile.open("data.txt"); char c; /* for every occurence of a character, incrementing the index of that character by one */ while(infile>>c) arr[int(c)]++; /* printing the character and its frequency */ for(int i=0;i<122;i+=4){ cout<<char(i)<<" - "<<arr[i]<<"\t\t"; cout<<char(i+1)<<" - "<<arr[i+1]<<"\t\t"; cout<<char(i+2)<<" - "<<arr[i+2]<<"\t\t"; if((i+3)<122) cout<<char(i+3)<<" - "<<arr[i+3]<<endl; } /* to find the most frequent and least frequent character */ int large=0,largeIndex,small=999999,smallIndex; for(int i=0;i<123;i++){ if(arr[i]>large){ large=arr[i]; largeIndex=i; } if(small>=arr[i]){ small=arr[i]; smallIndex=i; } } cout<<endl<<"Most frequent character is \'"<<char(largeIndex); cout<<"\' with frequency-"<<large<<endl; cout<<"Least frequent character is \'"<<char(smallIndex); cout<<"\' with frequency-"<<small<<endl; }
Output
Text File
Also read, Python3 How can I improve my code follow the instruction?
can you tell me why “int large=0,largeIndex,small=999999,smallIndex;”
the value of large and small look like this, still confused myself. Thank you.