Using visual studio , write c program The most common letter in the English lana
ID: 3879390 • Letter: U
Question
Using visual studio , write c program
The most common letter in the English lanague is ‘e’ which composes 12.702% of the average sentence.
Let’s create a program that tests this on a string.
Create a string (array of chars) that can hold 1000 characters.
Prompt the user to enter a string.
Calculate the frequency of the letter e within the string.
TIPS
Look back at the string example.
You want to walk through the string using repetition (probably a while loop).
Go from start to when the ‘’ character is seen.
You’ll need two counter variables.
The first will maintain your position in the string, from 0 to whenever the ‘’ character is seen.
The second will count every time an e character is seen.
TIP: Don’t forget to count if the character is ‘e’ OR ‘E’.
Calculate the frequency: Frequency = (eCounter / stringLength) * 100
TIP: you may need to type cast!
TIP: String length is up until '' is seen, NOT 1000!
If you know how to use the strlen() function, you may do so.
start with
#include<stdio.h>
#include<stdlib.h>
use printf , scanf
Enter your string less than 1000 characters): Here is some random string that I wrote Frequency of e character: 10.26% Press any key to continue . .Explanation / Answer
Below is the visual studio, c program for the given problem to count the frequency of character e in the given string entered by the use. I have used type casting and a for loop with two counters to do the job. Also, strlen function is used.
#include <stdio.h>
#include <ctype.h> // header file for tolower() fucntion
int main()
{
char str[1000]; // array of chars that can hold 1000 characters..
int i, lenOfString ,count = 0;
float frequency = 0.0;
printf("Enter your string <less than 1000 characters>: "); //Prompt the user to enter a string.
gets(str); // Get a string from console and store it in str array
lenOfString = strlen(str); // calculate the length of str excluding the '' at end.
// loop to iterate over string until last character('') is found.
for(i = 0; str[i] != ''; ++i)
{
if('e' == tolower(str[i])) // convert current character to lowercse and checks if it is equal to 'e' .
++count; // Increae count if it is .
}
frequency = ((float)count /lenOfString)*100; // Calculate the frequency of the letter e within the string.
printf("Frequency of e character : %.2f %%", frequency); // %.2f is used to print frequency upto two places after //the decimal point . %% is used to print % in output.
return 0;
}
Notes:
strlen() : The strlen() function calculates the length of a given string.It does not count the last charcter which is '' in a string.
tolower():The function takes an uppercase alphabet and converts it to a lowercase character. it returns the same character if it is already in the lower case.It is defined in ctype.h header file.
Typecasting : Typecasting just tells the compiler to pretend the data you have is a different data type to what it actually is so that you can do math in the required form. Integer division truncates, so (count/length) results in 0. since the count is mostly smaller than length.You need to cast it to float so that you get the desired result in correct format.
Note :The two counter are i and count. i is used to iterate over the string and count is used to store the count of e encountered in the string.
Hope this helps. If any doubts feel free to ask.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.