Skip to content
Gallery
CS480 Notes
Share
Explore
Course Playbook

icon picker
Project files

Last edited 191 days ago by System Writer
Project requirements/assignment page.
In this programming assignment, students need to count the frequency of each alphabet letter (a-z, total 26 case insensitive) and five case special characters (',' '.' ':' ';' '!') in all the .txt files under a given directory.

Assignment

You will be provided a zero.zip which includes the following files:
makefile
count.h
specialcount.c
alphabetcount.c
testalphabetcount.c
testspecialcount.c
README
Your assignment is to write/modify code in the following files
count.h
makefile
alphabetcount.c to count the frequency of alphabet letters; and
specialcount.c to count the frequency of special characters.
README to explain your code and include your test result
Test.log to record your terminal session when testing your final source code just before submission.
result
Note: Feel free to do any change of these files (fill the code, create new functions and etc). You also can create new .h and .c files if needed.
The instructor will use the following files to test your code, so please DON’T modify these two files:
testalphabetcount.c
testspecialcount.c
your program must execute correctly on Edoras machine, the instructor/TA will type the following commands to test your code:
make testalphabet // generate testalphabet executable file
make testspecial // generate testspecial executable file
./testalphabet // run the testalphabet program to count frequency of alphabet letters, the result will be stored in the file result.txt under result folder
./testspecial // run the testspecial program to count frequency of the five special words, the result will be stored in the file specialresult.txt under result folder.

Directions to complete your assignment

Login Edoras machine and create a new folder called programming under your account
Copy zip and data.zip from the shared folder cs480-02 on Edoras server to your newly created programming folder
Unzip the zero.zip and data.zip on edoras using the commands:
unzip zero.zip
unzip

When finished:
Create a new folder named ‘result’ under the programming folder
Modify source files under folder zero to complete this assignment
Test your program to make sure it works correctly.
Re-test your program following the commands provided above and record the terminal session to Test.log
Write README file to explain your code and include your test result
. └── programming ├── data │   ├── input_00.txt │   ├── input_01.txt │   ├── input_02.txt │   ├── input_03.txt │   ├── input_04.txt │   ├── input_05.txt │   ├── input_06.txt │   ├── input_07.txt │   ├── input_08.txt │   ├── input_09.txt │   ├── input_10.txt │   ├── input_11.txt │   ├── input_12.txt │   ├── input_13.txt │   ├── input_14.txt │   ├── input_15.txt │   ├── input_16.txt │   ├── input_17.txt │   ├── input_18.txt │   ├── input_19.txt │   ├── input_20.txt │   ├── input_21.txt │   ├── input_22.txt │   ├── input_23.txt │   ├── input_24.txt │   ├── input_25.txt │   ├── input_26.txt │   ├── input_27.txt │   ├── input_28.txt │   ├── input_29.txt │   └── input_30.txt └── zero ├── alphabetcount.c ├── count.h ├── makefile ├── specialcharcount.c ├── testalphabetcount.c └── testspecialcharcount.c
megaphone

Makefile

# A Makefile for Program 0, CS480

all: testalphabet testspecial

OTHER_ALPHA__OBJS = testalphabetcount.o alphabetcount.o
OTHER_SPECIAL__OBJS = testspecialcharcount.o specialcharcount.o

CC = gcc
CFLAGS = -g -std=c99

testalphabet: ${OTHER_ALPHA__OBJS}
${CC} -o testalphabet ${OTHER_ALPHA__OBJS}


testspecial: ${OTHER_SPECIAL__OBJS}
${CC} -o testspecial ${OTHER_SPECIAL__OBJS}

${OTHER_SPECIAL__OBJS} ${OTHER_SPECIAL__OBJS}:count.h

clean:
rm -f *.o testalphabet testspecial

info

count.h

/* charcount.h - This header file include type definitions (including function prototypes) and macros
used for the programing assignment zero.
*/

#include <stdio.h>

#define ALPHABETSIZE 26 //The total number of alphabetical letter from a - z (case insensitive)
#define SPECIALCHARSIZE 5 // The number of these special characters ',' '.' ';' ':' '!'

void alphabetlettercount(char *path, char *filetowrite, long alphabetfreq[]);
/**
The alphabetlettercount function counts the frequency of each alphabet letter (a-z, case insensitive) in all the .txt files under
directory of the given path and write the results to a file named as filetowrite.
Input:
path - a pointer to a char string [a character array] specifying the path of the directory; and
filetowrite - a pointer to a char string [a character array] specifying the file where results should be written in.
alphabetfreq - a pointer to a long array storing the frequency of each alphabet letter from a - z:
alphabetfreq[0]: the frequency of 'a'
alphabetfreq[1]: the frequency of 'b'
... ...
alphabetfreq[25]:the frequency of 'z'

Output: a new file named as filetowrite with the frequency of each alphabet letter written in
*/

void specialcharcount(char *path, char *filetowrite, long charfreq[]);
/**
The specialcharcount function counts the frequency of the following 5 special characters:
',' '.' ':' ';' '!'
in all the .txt files under directory of the given path and write the results to a file named as filetowrite.
Input:
path - a pointer to a char string [a character array] specifying the path of the directory; and
filetowrite - a pointer to a char string [a character array] specifying the file where results should be written in.
alphabetfreq - a pointer to a long array storing the frequency of the above 5 special characters
charfreq[0]: the frequency of ','
charfreq[1]: the frequency of '.'
charfreq[2]: the frequency of ':'
charfreq[3]: the frequency of ';'
charfreq[4]: the frequency of '!'

Output: a new file named as filetowrite with the frequency of the above special characters written in
Steps recommended to finish the function:
1) Find all the files ending with .txt and store in filelist.
2) Read all files in the filelist one by one and count the frequency of each alphabet letter only (a - z). The array
long alphabetfreq[] always has the up-to-date frequencies of alphabet letters counted so far. If the letter is upper case, convert
it to lower case first.
3) Write the result in the output file: filetowrite in following format:
character -> frequency
example:
, -> 20
. -> 11
: -> 20
; -> 11
! -> 12
Assumption:
1) You can assume there is no sub-directory under the given path so you don't have to search the files
recursively.
2) Only .txt files are counted and other types of files should be ignored.
*/

ok

alphabetcount.c

/*
* alphabetcount.c - this file implements the alphabetlettercount function.
*/

#include <stdio.h>
#include "count.h"

/**
The alphabetlettercount function counts the frequency of each alphabet letter (a-z, case insensitive) in all the .txt files under
directory of the given path and write the results to a file named as filetowrite.
Input:
path - a pointer to a char string [a character array] specifying the path of the directory; and
filetowrite - a pointer to a char string [a character array] specifying the file where results should be written in.
alphabetfreq - a pointer to a long array storing the frequency of each alphabet letter from a - z:
alphabetfreq[0]: the frequency of 'a'
alphabetfreq[1]: the frequency of 'b'
... ...
alphabetfreq[25]:the frequency of 'z'

Output: a new file named as filetowrite with the frequency of each alphabet letter written in
Steps recommended to finish the function:
1) Find all the files ending with .txt and store in filelist.
2) Read all files in the filelist one by one and count the frequency of each alphabet letter only (a - z). The array
long alphabetfreq[] always has the up-to-date frequencies of alphabet letters counted so far. If the letter is upper case, convert
it to lower case first.
3) Write the result in the output file: filetowrite in following format:
letter -> frequency
example:
a -> 200
b -> 101
... ...
Assumption:
1) You can assume there is no sub-directory under the given path so you don't have to search the files
recursively.
2) Only .txt files are counted and other types of files should be ignored.
*/
void alphabetlettercount(char *path, char *filetowrite, long alphabetfreq[])
{
// TO-DO: please fill your code here ...
}

ok

specialcharcount.c

/*
* specialcharcount.c - this file implements the specialcharcount function.
*/


#include <stdio.h>
#include "count.h"

void specialcharcount(char *path, char *filetowrite, long charfreq[]) {

// TO-DO: please fill your code here ...
//pointers to the dir file
struct dirent *firstDir;
char txt[] = ".txt";
int txtLength = strlen(txt);
char *pathToFile;
char chr;
FILE * stream;
char pathToFile[512];
int i;
char specialChars[] = {',', '.', ':', ';', '!'};
int fileLength;

//open the directory
DIR *dir = opendir(path);

//check if dir
if(dir == NULL){
printf("Error: Directory not found");
return;
}

//grab files from dir
while ((firstDir = readdir(dir)) != NULL) {
//check if file is a .txt file
fileLength = strlen(firstDir->d_name);
if (fileLength <=textLength) {

} else {
pathToFile = firstDir -> d_name + fileLength - txtLength;


}
}
}
/**
The specialcharcount function counts the frequency of the following 5 special characters:
',' '.' ':' ';' '!'
in all the .txt files under directory of the given path and write the results to a file named as filetowrite.
Input:
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.