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

Need Some Help Please! #1 or #2 is fine.. #1 - Write a program that opens a user

ID: 665908 • Letter: N

Question

Need Some Help Please!

#1 or #2 is fine..

#1 - Write a program that opens a user given text file and does the following:
     1) counts the number of characters in the file
     2) counts the number of "lines" in a file (a "line" is any series of characters ending with a newline ' ')
     3) counts the number of words in the file (any sequence of non-white-space characters)

Your program should output all three values in a "user-friendly" way. Run this with one text file. Turn in your source code, the printed text file and a printout of the output.
HINT:This will be easier if you open the file and read it character-by-character first. This will allow you to easily count the characters and newlines. THEN close the file, reopen it and read through it using fscanf. When you tell fscanf to get a string, it only gets the characters UP TO the first white-space character.

---------------------OR--------------------------------

#2 - Write a program that combines the contents of two existing text files into one new file. The program should ask the user for the names of the two files. It will be assumed that the two files are in the same folder as the program. A new file will be created and written containing the two files in the order they were provided. You do not have to turn in a run for this program.


Using Codeblocks, C programming. Formatted and as simple as it can be would be to code, will be really nice!

Explanation / Answer

Probably i can give you two solutions for the Question1 :

Here is the the first way :

#include<stdio.h>
#include<string.h>
main(int argc,char* argv[])
{
FILE* fp;
int w=0, c=0, l=0, space=1;
char ch;
if(argc<2)
{
printf("COMMAND ERRORn");
return;
}
if(argc>3)
{
printf("COMMAND ERRORn");
return;
}
if(argc==3)
{
fp = fopen(argv[2],"r");
if(fp==NULL)/*File does not exist or it cannot be opened successfully*/
{
printf("ERROR in opening filen");
return;
}
}
else
{
fp=fopen(argv[1],"r");
if(fp==NULL)/*File does not exist or it cannot be opened successfully*/
{
printf("ERROR in opening filen");
return;
}
}
ch=fgetc(fp);
while(!feof(fp))
{
if(ch==' ')
{
space=1;
}
else
if(ch=='n')
{
l++;
space=1;
}
else
{
if(space==1)
w++;
space=0;
}
c++;
ch=fgetc(fp);
}
/*Checking which information user wants and then print the corresponding info. along with the filename*/
if(argc==3)
{
if(!strcmp(argv[1],"-w"))
printf("%3d",w);
else if(!strcmp(argv[1],"-l"))
printf("%3d",l);
else if(!strcmp(argv[1],"-c"))
printf("%3d",c);
printf(" %sn",argv[2]);
}
else/*Print number of lines,words and characters of the file along with the filename*/
{
printf("%3d",l);
printf("%3d",w);
printf("%3d",c);
printf(" %sn",argv[1]);
}
fclose(fp);
}

You can also follow the second approach which is mentioned below which meets the same purpose

#include<stdio.h>
#include<conio.h>
void main()
{
int noc=0,now=0,nol=0;
FILE *fw,*fr;
char fname[20],ch;
clrscr();
printf(" enter the source file name");
gets(fname);
fr=fopen(fname,"r");
if(fr==NULL)
{
printf(" error ");
exit(0);
}
ch=fgetc(fr);
while(ch!=EOF)
{
noc++;
if(ch==' ');
now++;
if(ch==' ')
{
nol++;
now++;
}
ch=fgetc(fr);
}
fclose(fr);
printf(" total no of character=%d",noc);
printf(" total no of words=%d",now);
printf(" total no of lines=%d",nol);
getch();
}

Hope it really helps you!

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote