Write a C program (on a Unix/Linux system) that counts and displays the number o
ID: 3563839 • Letter: W
Question
Write a C program (on a Unix/Linux system) that counts and displays the
number of words, non-white space characters, and/or lines in a text file. The
name of the input file is to be provided to the program as a command line
argument. In addition, the program takes option flags to determine what actions
to take. The command format is as follows:
command [-clw] inputfile
where the presence of a flag indicates the following actions:
c: display the number of characters in inputfile.
l: display the number of lines in inputfile.
w: display the number of words in inputfile.
Flags can be specified in any order. If no flags are specifyed, the command will
by default display the number of characters, words, and lines. For any other
format, the program should generate an appropriate error message. Similarly, if
the input file can not be read the program should generate an appropriate error
message.
Explanation / Answer
====================================
Program is:
====================================
#include <stdio.h>
#include <string.h>
int main()
{
FILE *fp;
char buff[255];
char command[255];
int c=0,i,j=0;
int line=0;
int word=0;
char ch;
char fileName[255];
printf("********************************************* ");
printf("Enter command (Example -c input.txt) ");
printf("-c for Number of character in file ");
printf("-w for Number of words in file ");
printf("-l for Number of lines in file ");
printf("********************************************* ");
scanf("%s",command);
//Extracting file name
if(command[2]==' ') {
if(command[1]=='l'||command[1]=='w'||command[1]=='c') {
while(command[i]!='')
{
if(i>2) {
fileName[j]=command[i];
j++;
}
i++;
}
fileName[j]='';
} else {
printf("Invalid command provided , Plese provide [-clw] only ");
return;
}
} else {
while(command[i]!='')
{
fileName[i]=command[i];
i++;
}
fileName[i]='';
}
printf("********************************************* ");
printf("File Name You have provided is:%s ",fileName);
printf("********************************************* ");
fp = fopen(fileName, "r");
if( fp != NULL ){
while ( !feof(fp ) ){
memset(buff, '', sizeof( buff) );
fgets(buff, 255, (FILE*)fp);
for(i = 0; buff[ i ]; i++)
{
if (buff[i] == ' '){
line ++,word++;
} else if (buff[i] == ' '){
word ++;
} else {
c++;
}
}
}
fclose(fp);
if(command[0]=='-' && command[1]=='w') {
printf("Number of words in file: %i ", word);
}
else if(command[0]=='-' && command[1]=='c') {
printf("Number of characters in file: %i ",(c-2));
}
else if(command[0]=='-' && command[1]=='l') {
printf("Number of lines in file: %i ", line);
} else {
printf("Number of words in file: %i ", word);
printf("Number of characters in file: %i ",(c-2));
printf("Number of lines in file: %i ", line);
}
printf("********************************************* ");
} else {
printf("File not found");
}
return;
}
=========================================
Output
=========================================
Case1:
case 2:
case3:
case4:
case5:
===================================================
input.txt file content
==================================================
ab
df ed
ef
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.