Please write the code is C. Thank You! 1) Counting words and characters (50 poin
ID: 3907240 • Letter: P
Question
Please write the code is C. Thank You!
1) Counting words and characters (50 points) Write a C program, count.c, which counts the number of words, characters, and frequency of each character in the user provided sentence. Your program must: Ask user to enter a sentence (accept input until user hits "enter"). Assume that the sentence is no longer than 200 characters. Count the number of words, assume that words are separated by a single space character. Count the number of characters and also the frequency of each character (including spaces and special characters). Do not display count for characters that don't exist in the user input. 1. 2. 3. Example executions: $ gcc -Wall -o count count.c $ ./count Enter a sentence: Hello Your sentence has 1 word (s) Your sentence has 5 character (s) Your sentence has l H Your sentence has 1e Your sentence has 2 1 Your sentence has 1 o ./count Enter a sentence: I am in class! Your sentence has 4 word(s) Your sentence has 14 character (s) Your sentence has 3 Your sentence has 1! Your sentence has 1I Your sentence has 2 a Your sentence has 1c Your sentence has 1 i Your sentence has 11 Your sentence has m Your sentence has 1 n Your sentence has 2 sExplanation / Answer
ScreenShot
-------------------------------------------------
Progam(count.c)
//Header files for I/O and string handling
#include <stdio.h>
#include <string.h>
//Main method
void main()
{
//Array of char for sentence store
char s[100];
//Variables for counting
int countWord = 0, i, countChar=0,space=0,exclam=0,period=0;
int freqChar[26];
//set 0 to store fequency of all alphabet
for (i = 0; i<26; i++)
{
freqChar[i] = 0;
}
//Sentence enter prompt
printf("Enter a sentence:");
scanf("%[^ ]s", s);
//Loop for word ,charcter count
for (i = 0; s[i] != ''; i++)
{
//Word and space count
if (s[i] == ' ') {
countWord++;
space++;
}
//Exclamation count
if (s[i] == '!') {
exclam++;
}
//Period count
if (s[i] == '.') {
period++;
}
//Character count
countChar++;
//Alphabet count
if (s[i] >= 'a' && s[i] <= 'z')
{
freqChar[s[i] - 97]++;
}
else if (s[i] >= 'A' && s[i] <= 'Z')
{
freqChar[s[i] - 65]++;
}
}
//Display details
printf("Your sentence has %d word(s) ", countWord + 1);
printf("Your sentence has %d characters(s) ", countChar);
if (space > 0) {
printf("Your sentence has %d ", space);
}
if (exclam > 0) {
printf("Your sentence has %d ! ", exclam);
}
if (period > 0) {
printf("Your sentence has %d . ", exclam);
}
for (i = 0; i<26; i++)
{
if (freqChar[i] != 0)
{
printf("Your sentence has %d %c ", freqChar[i], (i + 97));
}
}
}
Output
Enter a sentence:hello world
Your sentence has 2 word(s)
Your sentence has 11 characters(s)
Your sentence has 1
Your sentence has 1 d
Your sentence has 1 e
Your sentence has 1 h
Your sentence has 3 l
Your sentence has 2 o
Your sentence has 1 r
Your sentence has 1 w
Press any key to continue . . .
--------------------------------------------
Program(sort.c)
//Header files for I/O
#include<stdio.h>
#include <stdlib.h>
//main method
void main()
{
//Variable for ascend/descend choice
char ch;
//Variable for store array values and sort
int *arr, i, j, temp;
//allocate array size
arr = (int *)malloc(4 * sizeof(int));
//Prompt to enter details
printf("sort[a]|d]<list of numbers>");
scanf("%c %d %d %d %d", &ch, arr + 0, arr + 1, arr + 2, arr + 3);
//Ascending order
if (ch == 'a') {
for (i = 0; i < 4; ++i)
{
for (j = i + 1; j < 4; ++j)
{
if (*(arr + i) > *(arr + j))
{
temp = *(arr + i);
*(arr + i) = *(arr + j);
*(arr + j) = temp;
}
}
}
for (int i = 0; i < 4; i++) {
printf("%d ", *(arr + i));
}
printf(" ");
}
//descending order
else if (ch == 'd') {
for (i = 0; i < 4; ++i)
{
for (j = i + 1; j < 4; ++j)
{
if (*(arr + i) < *(arr + j))
{
temp = *(arr + i);
*(arr + i) = *(arr + j);
*(arr + j) = temp;
}
}
}
for (int i = 0; i < 4; i++) {
printf("%d ", *(arr + i));
}
printf(" ");
}
}
Output
sort[a]|d]<list of numbers>d -8 1 4 7
7 4 1 -8
Press any key to continue . . .