Docsity

C Program to Count Digits and Letters Frequency and Write the Most Frequent One - Prof. Ch, Study Guides, Projects, Research of Computer Science

Toggle sidebar
bg1
CSC 3320 Project 3
Note: Submit before midnight of April 26. More importantly, no late submission is accepted.
Requirements:
Write a C program which reads the file “input.txt” and counts the occurrence frequency of each digit (0,
1, 2, ..., 9) and letter (A/a, B/b, …, Z/z). Then, use fork() to duplicate a child process to write the most
frequent digit and letter at the end of the file “input.txt”. The parent process output the occurrence
frequency of each digit and letter to the terminal (using “printf”). You must invoke the function
“mostfrequent” (which is defined as follows) in your program (it can be used for calculating the most
frequent digit and letter).
int mostfrequent(int * a, int length)/*length is the length of the array. The function returns the index of
the largest value of array a.*/
{
int index=0;
int max=a[0];
int i;
for(i=1;i<length;i++)
if (a[i]>max)
{
max = a[i];
index = i;
}
return index;
}
You can download the demo of Project 3 from the following link.
http://www.cs.gsu.edu/~chunyuai/3320/project/project3
Running example:
[chunyuai@grid ~]$ cat input.txt
Connecting workds.
Most acaemic writting is make up of complex sentences.
pf3
pf4
pf5

Partial preview of the text

Download C Program to Count Digits and Letters Frequency and Write the Most Frequent One - Prof. Ch and more Study Guides, Projects, Research Computer Science in PDF only on Docsity!

CSC 3320 Project 3

Note: Submit before midnight of April 26. More importantly, no late submission is accepted.

Requirements:

Write a C program which reads the file “input.txt” and counts the occurrence frequency of each digit (0,

1, 2, ..., 9) and letter (A/a, B/b, …, Z/z). Then, use fork() to duplicate a child process to write the most

frequent digit and letter at the end of the file “input.txt”. The parent process output the occurrence

frequency of each digit and letter to the terminal (using “printf”). You must invoke the function

“mostfrequent” (which is defined as follows) in your program (it can be used for calculating the most

frequent digit and letter).

int mostfrequent(int * a, int length)/length is the length of the array. The function returns the index of the largest value of array a./

{

int index=0; int max=a[0]; int i; for(i=1;i<length;i++) if (a[i]>max) { max = a[i]; index = i; } return index;

}

You can download the demo of Project 3 from the following link.

http://www.cs.gsu.edu/~chunyuai/3320/project/project

Running example:

[chunyuai@grid ~]$ cat input.txt

Connecting workds.

Most acaemic writting is make up of complex sentences.

  • 12 39 ‐0
  • [chunyuai@grid ~]$ ./project
  • The number of '0' is
  • The number of '1' is
  • The number of '2' is
  • The number of '3' is
  • The number of '4' is
  • The number of '5' is
  • The number of '6' is
  • The number of '7' is
  • The number of '8' is
  • The number of '9' is
  • The number of 'A' and 'a' is
  • The number of 'B' and 'b' is
  • The number of 'C' and 'c' is
  • The number of 'D' and 'd' is
  • The number of 'E' and 'e' is
  • The number of 'F' and 'f' is
  • The number of 'G' and 'g' is
  • The number of 'H' and 'h' is
  • The number of 'I' and 'i' is
  • The number of 'J' and 'j' is
  • The number of 'K' and 'k' is
  • The number of 'L' and 'l' is
  • The number of 'M' and 'm' is

Appendix

/counting occurring frequency/ #include <stdio.h>

int mostfrequent(int * a, int length)/length is the length of the array. The function returns the index of the largest value of array a./ { int index=0; int max=a[0]; int i; for(i=1;i<length;i++) if (a[i]>max) { max = a[i]; index = i; } return index; }

int main() { char c; int i, nwhite, nother; int ndigit[10];

nwhite = nother = 0; for (i=0; i<10; ++i) ndigit[i] = 0; while ((c = getchar()) != EOF) if (c >= '0' && c <= '9') ++ndigit[c‐'0']; else if (c == ' ' || c == '\n' || c == '\t') ++nwhite; else ++nother; printf("digits = "); for (i=0; i<10; ++i) printf("%d ",ndigit[i]); printf(", white space = %d, other = %d\n",nwhite, nother); printf("The most frequent digit is '%c'\n",'0'+mostfrequent(ndigit,10)); }

/*Process and file read and write. */ #include <unistd.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <stdio.h> #include <stdlib.h> #include <string.h>

int main(int argc, char * argv[]) {

int fd; char c; int i; int pid; char str[1000];

if(argc>1) /argc is the number of command line parameters./ fd=open(argv[1], O_RDWR, 0); /argv[1] is the first command line parameter./ else fd=open("input.txt", O_RDWR, 0);/if no command line parameter, open file input.txt/ if (fd==‐1) { printf("Open file failed.\n"); return(0); } else { while (read(fd,&c,1)) { printf("I read a letter '%c'\n",c); } } pid=fork(); /duplicate a process/ if (pid==0) /child process/ { memset(str,'\0',1000); /init str to be empty./ strcat(str,"\nThe child process writes the file.\n"); write(fd,str,strlen(str)); close(fd); } else /parent process/ { printf("\n The parent process outputs to the terminal.\n" ); } return(1);