Question

Write a program that calculates the frequency of letter occurrences in text. Read ASCII text from...

  1. Write a program that calculates the frequency of letter occurrences in text.
  2. Read ASCII text from standard input.
  3. On reaching EOF, print to stdout the normalized frequency of occurrence for each letter a-z that appeared in the input, one per line, in alphabetical order using the format produced by
    printf( "%c %.4f\n", letter, freq);
  4. Letters that occur zero times should not appear in the output.
  5. Characters other than lower and upper case letters should be ignored.
  6. Lower and upper case instances count as the same letter, e.g. 'a' and 'A' are both reported for the letter 'a' on the output.
  7. The frequencies reported should sum to approximately 1 (with a little slack for accumulation of printf rounding errors).
  8. By the way, you cannot implement this function by writing 26 "if" statements (1 for each letter). Hint: Each letter has a numerical ASCII value. Can this numerical value be used at all?

      Example Runs:

$ ./lfreq
aaab
a 0.7500
b 0.2500
$ ./lfreq
q
q 1.0000
./lfreq < happy_prince.txt
a 0.0841
b 0.0140
c 0.0206
...
y 0.0240
z 0.0002
$ ./lfreq < "large novel in English.txt"
a 0.0817
b 0.0149
c 0.0278
...
y 0.0197
z 0.0001

Language is C. Please avoid the use of break and continue.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program Files:

Letter Frequency.c

#include <stdio.h>

int main(void)
{
int c;
int alphabet[26];
int temp = 0;
int letterCount = 0;
float freq = 0;
int letter = 0;

for (int x = 0; x < 26; x++)
{
alphabet[x] = 0;
}
int x = 0;

while (c != EOF)
{

c = getchar();
x = 0;
temp = 'a';

if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
{
letterCount++;
}
while (c >= temp && temp >= 'a' && temp <= 'z')
{
if (c == temp)
{
alphabet[x]++;
}
x++;
temp++;
}
temp = 'A';
while (c >= temp && temp >= 'A' && temp <= 'Z')
{
if (c == temp)
{
alphabet[x]++;
}

x++;
temp++;
}
}
letter = 'a';
x = 0;
while (letter >= 'a' && letter <= 'z')
{
if (alphabet[x] != 0)
{
freq = (float)alphabet[x] / (float)letterCount;
printf("%c %.4f\n", letter, freq);
}
x++;
letter++;
}

return 0;
}

output:

 clang-7 main.c
 ./a.out < happy_prince.txt
a 0.0841
b 0.0140
c 0.0206
d 0.0448
e 0.1291
f 0.0199
g 0.0205
h 0.0713
i 0.0661
j 0.0009
k 0.0078
l 0.0540
m 0.0201
n 0.0602
o 0.0714
p 0.0195
q 0.0012
r 0.0536
s 0.0618
t 0.0891
u 0.0237
v 0.0076
w 0.0342
x 0.0005
y 0.0240
z 0.0002

Hope this helps!

Please let me know if any changes needed.

Thank you!

Add a comment
Know the answer?
Add Answer to:
Write a program that calculates the frequency of letter occurrences in text. Read ASCII text from...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a program that counts the frequency of each letter of the alphabet as they occur...

    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....

  • java programming Problem 2 (25 points) Counting Character Frequency. Write a program to analyze a text...

    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...

  • Write a complete C program that inputs a paragraph of text and prints out each unique...

    Write a complete C program that inputs a paragraph of text and prints out each unique letter found in the text along with the number of times it occurred. A sample run of the program is given below. You should input your text from a data file specified on the command line. Your output should be formatted and presented exactly like the sample run (i.e. alphabetized with the exact spacings and output labels). The name of your data file along...

  • Using basic c++ write 2 separate codes for this assignment. Program #1 Write a program that...

    Using basic c++ write 2 separate codes for this assignment. Program #1 Write a program that calculates the average of a group of test scores, where the lowest score in the group is dropped. It should use the following functions. • void getScore() should ask the user for a test score, store it in the reference parameter variable, and validate it. This function should be called by the main once for each of the five scores to be entered. •...

  • C program: Write a program that assigns and counts the number of each alphabetic character in...

    C program: Write a program that assigns and counts the number of each alphabetic character in the Declaration of Independence and sorts the counts from the most used to the least used character. Consider upper and lower case letters the same. The frequency of each character should be accumulated in an array. USE POINTERS to increment counts for each letter, sort your array, and display the results. Output should consist of two columns: (1) each letter 'a'-'z' and (2) the...

  • create a Java class ShiftCipher The program ShiftCipher should take two inputs from the terminal. The...

    create a Java class ShiftCipher The program ShiftCipher should take two inputs from the terminal. The first should be a string of any length which contains any type of symbol (the plaintext). The second will be a shift value which should be between 0 and 25 inclusive (though you may design your program to be resilient to shifts beyond this value). The program should print the cipher text, in which each of the alphabetic characters in the string is shifted...

  • How do i write a program to read muliple files? i can read one but i...

    How do i write a program to read muliple files? i can read one but i need to read 10: 0.txt - 9.txt Here is the code for reading one but i need to read 10 text files and print the frequencies of all the letters a - z, upper and lowercase, in one take. here is the output i should get #include <stdio.h> #include <stdlib.h> #include <string.h> #include <omp.h> int main() { double start_time = omp_get_wtime(); char str[1000); int...

  • c program that counts the number of characters, words and lines from standard input until EOF....

    c program that counts the number of characters, words and lines from standard input until EOF. attached is what i Have so far but its not working ?. about shell redirection Requirements 1. Write a C program that counts the number of characters, words and lines read from standard Input until EOF Is reached. 2. Assume the Input is ASCII text of any length. 3. Every byte read from stdin counts as a character except EOF 4. Words are defined...

  • Conditional statements, loops, reading from file, user defined functions.

    # in C Project objective: Conditional statements, loops, reading from file, user defined functions.**Submit source code (prog3.c) through CanvasOne source code file(unformatted text) will be submittedHere is INCOMPLETE code to get started: prog3.cHere is input.txt file: input.txtThe file name must match the assignmentThe code should be tested and run on a Microsoft compiler before it is uploaded onto CanvasThe code must be submitted on time in order to receive credit (11:59PM on the due date)Late submissions will not be accepted or gradedAll...

  • 1) Echo the input: First, you should make sure you can write a program and have...

    1) Echo the input: First, you should make sure you can write a program and have it compile and run, take input and give output. So to start you should just echo the input. This means you should prompt the user for the plaintext, read it in and then print it back out, with a message such as "this is the plaintext you entered:". [4 points, for writing a working program, echoing the input and submitting the program on the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT