alphabetcount.c /* * alphabetcount.c - this file implements the alphabetlettercount function. */ #include #inc
Posted: Sun Jul 03, 2022 11:22 am
alphabetcount.c
/*
* alphabetcount.c - this file implements thealphabetlettercount function.
*/
#include <stdio.h>
#include <dirent.h>
#include <string.h>
#include "count.h"
/**
The alphabetlettercount function counts the frequency ofeach alphabet letter (A-Z a-z, case sensitive) in all the .txtfiles under
directory of the given path and write the results to afile named as filetowrite.
Input:
path - a pointer to a char string [acharacter array] specifying the path of the directory; and
filetowrite - a pointer to a char string [acharacter array] specifying the file where results should bewritten in.
alphabetfreq - a pointer to a long arraystoring the frequency of each alphabet letter from A - Z a - z:
alphabetfreq[0]: the frequency of 'A'
alphabetfreq[1]: the frequency of 'B'
... ...
alphabetfreq[25]:the frequency of 'Z'
alphabetfreq[26]:the frequency of 'a'
... ...
alphabetfreq[51]:the frequency of 'z'
Output: a new file named as filetowrite with thefrequency of each alphabet letter written in
Steps recommended to finish the function:
1) Find all the files ending with .txt and store infilelist.
2) Read all files in the filelist one by one and countthe frequency of each alphabet letter only (A-Z a - z). Thearray
long alphabetfreq[] always has the up-to-date frequenciesof alphabet letters counted so far.
3) Write the result in the output file: filetowrite infollowing format:
letter -> frequency
example:
A -> 200
B -> 101
... ...
Assumption:
1) You can assume there is no sub-directory under thegiven path so you don't have to search the files
recursively.
2) Only .txt files are counted and other types of filesshould be ignored.
*/
// My code
void alphabetlettercount(char *path, char *filetowrite, longalphabetfreq[])
{
DIR *d;
char *filename;
char *fil;
struct dirent *dir; //directory opening process
d = opendir(path);
if(d != NULL) {
while((dir = readdir(d)) != NULL) {
filename = dir->d_name; //assigns current name to stringfilename
size_t t = strlen(filename) - 3; //checks for .txtextentsion
int ctr = 0;
while(t < strlen(filename)) {
if(!(filename[t] == 't' || filename[t] == 'x'))
continue;
else {
ctr++; //adds the current letter to a counter
}
t++;
}
if(ctr == 3) { //counter will only be 3 if "txt" is read
fil = dir->d_name; //immediately stores validated file to beread
char p[256];
strcpy(p, path); //concatenates the full data directory to p
strcat(p, "/");
strcat(p, fil);
FILE *f = fopen(p, "r"); //opens the file path for reading
printf("%s\n", filename);
if(f == NULL) { //can't open file, abort
return;
}
int c = fgetc(f); //grabs the first character
int temp;
while (c != EOF) {
c = fgetc(f);
if ((c >= 65) && (c <=90)){
temp = c - 65;
alphabetfreq[temp]++;
}
if ((c >= 97) && (c <=122)){
temp = c - 97+26;
alphabetfreq[temp]++;
}
}
fclose(f);
FILE *g = fopen(filetowrite, "w"); //opens result.txt forwriting
for(int i = 0; i < 26; i++) { //loops through entirealphabetfreq
fprintf(f, "%c -> %ld\n", (char)(i+97), alphabetfreq);//formatted writing
}
fclose(g);
}
}
}
closedir(d); //close directory
}
testalphabetcount.c
/* This program is to test alphabetcount function.
*
*/
#include <stdio.h>
#include <dirent.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include<unistd.h>
#include <stdbool.h>
#include "count.h"
/*
* Print the frequencies of alphabetical characters storedin array: charfreq[] to output screen in the format as:
* letter -> frequency (one letter a line)
* Input: charfreq - array having the frequency of eachalphabet letter
size - thetotal number of alphabet letters
* example:
* a -> 20
* b -> 30
*/
void displayalphabetfreq(long charfreq[], int size)
{
for(int i = 0; i < size/2; i++) // printupper case letter frequency
{
printf("%c -> %d\n",(char)(i+65), charfreq);
}
for(int i = size/2; i < size;i++) // print lower case letter frequency
{
printf("%c -> %d\n", (char)(i+65+ 6), charfreq);
}
}
int main()
{
char *path = "../data"; // the data *.txt files are under thisfolder
char *filetowrite = "../result/result.txt"; // the frequency of all alphabetical letters will bewritten in this file
long alphabetfreq[ALPHABETSIZE] = {0}; //array to store the frequency of each alphablet letter
alphabetlettercount(path, filetowrite,alphabetfreq); // process the data files
displayalphabetfreq(alphabetfreq,ALPHABETSIZE); // print the frequency stored in the array tooutput screen
}
Fix the alphabetcount.c code, MAKE SURE ITS INC
/*
* alphabetcount.c - this file implements thealphabetlettercount function.
*/
#include <stdio.h>
#include <dirent.h>
#include <string.h>
#include "count.h"
/**
The alphabetlettercount function counts the frequency ofeach alphabet letter (A-Z a-z, case sensitive) in all the .txtfiles under
directory of the given path and write the results to afile named as filetowrite.
Input:
path - a pointer to a char string [acharacter array] specifying the path of the directory; and
filetowrite - a pointer to a char string [acharacter array] specifying the file where results should bewritten in.
alphabetfreq - a pointer to a long arraystoring the frequency of each alphabet letter from A - Z a - z:
alphabetfreq[0]: the frequency of 'A'
alphabetfreq[1]: the frequency of 'B'
... ...
alphabetfreq[25]:the frequency of 'Z'
alphabetfreq[26]:the frequency of 'a'
... ...
alphabetfreq[51]:the frequency of 'z'
Output: a new file named as filetowrite with thefrequency of each alphabet letter written in
Steps recommended to finish the function:
1) Find all the files ending with .txt and store infilelist.
2) Read all files in the filelist one by one and countthe frequency of each alphabet letter only (A-Z a - z). Thearray
long alphabetfreq[] always has the up-to-date frequenciesof alphabet letters counted so far.
3) Write the result in the output file: filetowrite infollowing format:
letter -> frequency
example:
A -> 200
B -> 101
... ...
Assumption:
1) You can assume there is no sub-directory under thegiven path so you don't have to search the files
recursively.
2) Only .txt files are counted and other types of filesshould be ignored.
*/
// My code
void alphabetlettercount(char *path, char *filetowrite, longalphabetfreq[])
{
DIR *d;
char *filename;
char *fil;
struct dirent *dir; //directory opening process
d = opendir(path);
if(d != NULL) {
while((dir = readdir(d)) != NULL) {
filename = dir->d_name; //assigns current name to stringfilename
size_t t = strlen(filename) - 3; //checks for .txtextentsion
int ctr = 0;
while(t < strlen(filename)) {
if(!(filename[t] == 't' || filename[t] == 'x'))
continue;
else {
ctr++; //adds the current letter to a counter
}
t++;
}
if(ctr == 3) { //counter will only be 3 if "txt" is read
fil = dir->d_name; //immediately stores validated file to beread
char p[256];
strcpy(p, path); //concatenates the full data directory to p
strcat(p, "/");
strcat(p, fil);
FILE *f = fopen(p, "r"); //opens the file path for reading
printf("%s\n", filename);
if(f == NULL) { //can't open file, abort
return;
}
int c = fgetc(f); //grabs the first character
int temp;
while (c != EOF) {
c = fgetc(f);
if ((c >= 65) && (c <=90)){
temp = c - 65;
alphabetfreq[temp]++;
}
if ((c >= 97) && (c <=122)){
temp = c - 97+26;
alphabetfreq[temp]++;
}
}
fclose(f);
FILE *g = fopen(filetowrite, "w"); //opens result.txt forwriting
for(int i = 0; i < 26; i++) { //loops through entirealphabetfreq
fprintf(f, "%c -> %ld\n", (char)(i+97), alphabetfreq);//formatted writing
}
fclose(g);
}
}
}
closedir(d); //close directory
}
testalphabetcount.c
/* This program is to test alphabetcount function.
*
*/
#include <stdio.h>
#include <dirent.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include<unistd.h>
#include <stdbool.h>
#include "count.h"
/*
* Print the frequencies of alphabetical characters storedin array: charfreq[] to output screen in the format as:
* letter -> frequency (one letter a line)
* Input: charfreq - array having the frequency of eachalphabet letter
size - thetotal number of alphabet letters
* example:
* a -> 20
* b -> 30
*/
void displayalphabetfreq(long charfreq[], int size)
{
for(int i = 0; i < size/2; i++) // printupper case letter frequency
{
printf("%c -> %d\n",(char)(i+65), charfreq);
}
for(int i = size/2; i < size;i++) // print lower case letter frequency
{
printf("%c -> %d\n", (char)(i+65+ 6), charfreq);
}
}
int main()
{
char *path = "../data"; // the data *.txt files are under thisfolder
char *filetowrite = "../result/result.txt"; // the frequency of all alphabetical letters will bewritten in this file
long alphabetfreq[ALPHABETSIZE] = {0}; //array to store the frequency of each alphablet letter
alphabetlettercount(path, filetowrite,alphabetfreq); // process the data files
displayalphabetfreq(alphabetfreq,ALPHABETSIZE); // print the frequency stored in the array tooutput screen
}
Fix the alphabetcount.c code, MAKE SURE ITS INC