Background:
The first step towards helping someone 'decode' a message is often to count how many times each letter of the alphabet appears in the message. Then divide each count by the total number of letters to compute the relative 'frequency'. From these frequencies, it may be easier to recognize which letters are assigned to the vowels.
For your own reference, here is a table of standard letter frequencies from typical English text. (You do NOT need to display this to the user.)
letter frequency
For example, given this (encoded) message:
message: "K ukd, k yckd, k fkdkc, ykdkuk"
The letter counts and frequencies are as follows:
letter count frequency
Application:
Write a program in C or C++ to repeatedly ask the user for a file name, reads the file, then displays the original text, and displays the number of instances of each letter from a to z and the letter frequencies. Repeat that process until the user chooses to stop.
Implementation:
You must use a c-string (an array of characters with a null character on the end) to represent the message. You must also use an array (of exactly 26 integers) to represent the letter counters. These arrays must be declared in the main program and shared with the subprograms by passing them as parameters. For the message, declare an array of 501 characters.
Write functions for the following:
(1) A function that reads text from a file one character at a time (including white-space) and returns a string containing the text. To see the new-line characters, you will need to use the 'get' function or scanf function (instead of the more complicated >> operator). You cannot 'return' an array in C++, so the text string must be allocated externally and passed as a parameter. Ask the user to enter the file name. (Use the 'getLine' or scanf function.)
(2) A function that is given a string and an array of 26 character counters that computes how many times each letter appears in the string. Start by initializing the counters to zero. To access the array of character counters, compute the index from each character (first convert the letter to lower-case, if necessary, and then the index is just the letter 'a' subtracted from the given character). If the character is not a letter, just ignore it.
(3) A function given a character that returns the lower-case version if it is upper case, or the same character.
(4) A function given an array of character counters that displays the contents in a table, with letter frequencies, as described above. Hold the screen (wait for user input) after displaying the first thirteen letters (a-m), then display the rest and hold the screen again.
You must use the letters of the message to compute indexes into the array of letter counters. (You may NOT use an 'if' statement with 26 branches.) Most of all, do not create code using 'if' statements with large numbers of branches (more than 5).
When computing the letter counts, compute the index into the array of counters as follows:
index = letter - 'a';
The difference between 'b' and 'a' is 1, and the difference between 'c' and 'a' is 2, and so on. So you can take each letter and compute the index, then add one to the counter at that index.
When you make the table of letters, letter counts, and frequencies, you can also compute the letter from the array index, as follows:
char letter = (char) ('a' + index);
Do not use any library functions. Write your own versions.
Notes:
(a) A character is upper-case if its ASCII code is between 'A' and 'Z' inclusive.
(b) The difference between the ASCII code of a lower-case letter and the ASCII code for the corresponding lower-case letter is a constant. That constant difference can be represented as 'a' - 'A'. Adding that constant to the upper-case ASCII code will produce the corresponding lower-case ASCII code.
#include"stdio.h"
#include"string.h"
/* read from file */
char readingfile(char *_file,char *msg){
FILE *in;
char *ret;
char ch;
int i=0;
/* opening file in read mode */
in=fopen(_file,"r");
/* if file not found */
if(in==NULL){
printf("Could not open the file.\n");
strcpy(msg,"");
}
else{
/* read file by each character */
while((ch = fgetc(in)) != EOF){
msg[i]=ch;
i++;
}
/* add null character at end */
msg[i]='\0';
}
/* close file */
fclose(in);
}
/* change upper to lower case letter */
char lowerCase(char ch){
int dis=0;
/* check character */
if((ch>='a' && ch<='z') || (ch>='A' &&
ch<='Z')){
/* lower case character */
if(ch>='a' && ch<='z') {
dis=ch-'a';
}
/* upper case character */
else{
dis=ch-'A';
}
return ((char)('a'+dis));
}
return ch;
}
/* get number of characters */
int charCount(char *s,char ch){
int len=strlen(s);
int count=0;
int a;
for(a=0;a<len;a++){
/* check with lowercase letter
*/
if(lowerCase(s[a])==ch){
/* increase
count */
count++;
}
}
return count;
}
/* count update */
void setCharsCount(char *s,int *chCount){
int i;
char ch;
int dis;
int len=strlen(s);
/* set each character count to 0 */
for(i=0;i<26;i++){
chCount[i]=charCount(s,(char)('a'+i));
}
}
/* print characters count and percentage */
void printFreq(int *count){
int i;
int total=0;
/* calculate total */
for(i=0;i<26;i++){
total+=count[i];
}
/* character exist in string*/
if(total>0){
for(i=0;i<26;i++){
printf("%6c",((char)(i+'a')));
printf("%5d",count[i]);
/* calculate percentage */
printf("%5.1f%%",(count[i]*100/(float)total));
if(count[i]!=0){
printf("\t(%d/%d)",count[i],total);
}
printf("\n");
}
}
}
int main(){
int chCount[26];
char ignoreCh,msg[501],_file[30];
/* infinite loop */
while(1){
printf("Enter file name : ");
scanf("%s",_file);
/* read file */
readingfile(_file,msg);
/* set total character from msg */
setCharsCount(msg,chCount);
/* print */
printFreq(chCount);
printf("Want to read file again? [y/n]: ");
/* ignore enter */
ignoreCh=getchar();
/* user choice next file read */
ignoreCh=getchar();
if(ignoreCh=='N' || ignoreCh=='n'){
break;
}
printf("\n");
}
printf("\nGood Bye!\n");
return(0);
}

Background: The first step towards helping someone 'decode' a message is often to count how many...
My C++ program is not compiling. Please explain how you fixed with detailed inline comments. I am using Visual Studio 2017. It's a character count program that keeps and displays a count of all the upper, lower case and digits in a .txt file. The requirement is to use classes to implement it. -----------------------------------------------------------------HEADER FILE - Text.h--------------------------------------------------------------------------------------------- /* Header file contains only the class declarations and method prototypes. Comple class definitions will be in the class file.*/ #ifndef TEXT_H #define...
6.15 Program 6: Using Arrays to Count Letters in Text 1. Introduction In this program, you will practice working with arrays. Your program will read lines of text from the keyboard and use an array to track the number of times each letter occurs in the input text. You will use the contents of this array to generate a histogram (bar graph) indicating the relative frequencies of each letter entered. Test cases are available in this document. Remember, in addition...
Paste this code into a new file and find the errors. The most frequent letter in the user_string is H. # Function displays the character that appears most frequently # in the string. If several characters have the same highest # frequency, displays the first character with that frequency. def main(): # Set up local variables count = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' index = 0 frequent = 0 # Receive user input. user_string = 'Who where what why how' for ch...
JAVA Code: Complete the program that reads from a text file and counts the occurrence of each letter of the English alphabet. The given code already opens a specified text file and reads in the text one line at a time to a temporary String. Your task is to go through that String and count the occurrence of the letters and then print out the final tally of each letter (i.e., how many 'a's?, how many 'b's?, etc.) You can...
java programming Problem 2 (25 points) Counting Character Frequency. Write a program to analyze a text file (a novel or a report, or just a sequence of letters or symbols), by reading the file into a byte array, convert to a string, and then scan the string letter by letter to count the frequencies of all unique characters in the text, after that, the letters with frequencies greater than 0 and the frequencies are reported side by side. All members...
I need eclipse code for : Write a program that analyzes text written in the console by counting the number of times each of the 26 letters in the alphabet occurs. Uppercase and lowercase letters should be counted together (for example, both ‘A’ and ‘a’ should count as an A). Any characters that are not letters should be ignored. You must prompt the user to enter the text to be analyzed. Then, for any letter that appeared at least once...
Write a program that counts the frequency of each letter of the alphabet as they occur in a text contained in a text file. The program should read all the characters from a file and tally the frequency for each letter of the alphabet (case-insensitive), along with white space and total character count. Your program's data structure will eventually contain the 26 numbers that will represent the frequencies of occurrence of all 26 letters of the alphabet in that file....
Your country is at war and your enemies are using a secret code to communicate with each other.You have managed to intercept a message that reads asfollows::mmZdxZmx]ZpgyThe message is obviously encrypted using the enemy's secret code.You have just learned that their encryption method is based upon the ASCII code.Appendix 3 of your book shows the ACII character set.Individual characters in a string are encoded using this system. For example, the letter "A" isencoded using the number 65 and "B" is...
4. [8] Write a C function with prototype void letter_freq(const char wordll, int freaq ); This function computes the number of appearances of each letter in the string word and stores them irn array freq of size 26. The letters are the 26 letters of the Latin alphabet whose ASCII values are in the range 97-122 for the lower case letters, and in the range 65-90 for the uppercase letters. You must account for uppercase and lowercase letter variants, which...