Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

FILE pointers Command line arguments Character string manipulation Working with

ID: 3687876 • Letter: F

Question

FILE pointers

Command line arguments

Character string manipulation

Working with multiple files

Makefile

README

For this assignment, you will write several functions that will read in a file and manipulate and/or provide specific statistics about the text in the file. You will use FILE pointers to read and write to/from the files. Below are specific functions you need to provide with this assignment.

changeCase(You add parameters ) – This function will change the case of each character in the document. Ex. Look at each character in the document, if the character is lower case make it upper case and visa versa. Then print the changed text to the output file.

wordCount(You add parameters ) – This function counts the number of words in the documents and prints the information to the output file. An integer will be counted as a word.

sentenceCount(You add parameters ) – This function counts the number of sentences in the document and prints the information to the output file.

integerInfo(You add parameters ) – This functions identifies all integers that are in the document. It should do the following:

Print a list of the integers found

Print the sum of all the integers found

Print the average of all integers found

Below you will find the input file I used and the sample output for this text. You should create your own in.txt file to test your program.

PROGRAMMING IN C

Explanation / Answer

#include<stdio.h>
#include<process.h>
char ch;
int static nod[],i = 0, w = 0, s = 0, sum = 0;
float avg;
int main() {
FILE *fp1, *fp2;
clrscr();

fp1 = fopen("in.txt", "r");

if(fp1 == NULL) //if file does not exist, create it
{
fp1 = fopen("in.dat", "w");
printf(" Enter data to be stored in to the file:");
while((ch=getchar())!=EOF)
putc(ch,fp1);
  
return 0;
if (fp1 == NULL)
there_was_error = 1;
}
if (there_was_error)
{
printf("Disc full or no permission ");
return EXIT_FAILURE;
}


  
fp2 = fopen("out.txt", "w");
if (fp2 == NULL) {
puts("Not able to open this file");
fclose(fp1);
exit(1);
}

do {
a = fgetc(fp1);

if (isdigit(a)){ /* counting number of digits. */
nod[i] = a;
i++;
}
if (isalpha(a))
a = changeCase(a);

fputc(a, fp2);

} while (a != EOF);

integerInfo(nod);

w = wordCount(fp1);
fprintf(" Words in a file are=%d",w);
s = sentenceCount(fp1);
fprintf(" sentences in a file are=%d",s);
fcloseall();
getch();
}

char changeCase(char a)
{
return(islower(a)? toupper(a) :
tolower(a));
}

int wordCount(FILE *p)
{
ch=fgetc(p);
while(ch!=EOF){
fprintf("%c",ch);
if(ch==' '||ch==' ')
w++;
ch=fgetc(p);
}
return w;
}

int sentenceCount(FILE *p)
{
ch=fgetc(p);
while(ch!=EOF){
fprintf("%c",ch);
if(ch=='.')
s++;
ch=fgetc(p);
}
return s;
}
void integerInfo(int[] d)
{
for(int j= 0; j < i; j++){
fprintf("List of integers found are: %d",d[j]);
sum = sum+d[j];
}
avg = sum / i;
fprintf("Sum of all the integers found : %d", sum);
fprintf("Average of all integers found : %f", avg);
}