Write A C Program (NOT A C++ Program) Using ( microsoft ) Visual Studios 2015 I
ID: 3571850 • Letter: W
Question
Write A C Program (NOT A C++ Program) Using (microsoft ) Visual Studios 2015
I NEED THE PROGRAM WRITTEN USING MICROSOFT VISUAL STUDIOS 2015
A. Write a program that counts the number of Characters in a text file.
Intput: C programming
Output: There are 13 characters in the file f.txt
B. Write a program that counts the number of words in a text file.( A "word" is any sequence of non-white-space characters.)
Intput: C programming
Output: There are 2 words in the file f.txt
C.write a program that counts the number of lines in a text file.
Intput: C programming
C programming
Output: There are 2 lines in the file f.txt Have
Each Program MUST obtain the file name from the command line.
Explanation / Answer
Question A:
#include<stdio.h>
#include<stdlib.h>
int main(int argc, char *argv[]){
int k;
FILE *fp = fopen(argv[1], "r");
int c;
int count = 0;
if(fp != NULL){
while((c=fgetc(fp))) {
if(c == EOF) {break;}
count++;
}
printf("There are %d characters in the file %s ",count, argv[1]);
fclose(fp);
}
else{
printf("File does not exist ");
}
return 0;
}
output:
sh-4.2$ ./main f.txt
There are 13 characters in the file f.txt
Question B:
#include<stdio.h>
#include<stdlib.h>
int main(int argc, char *argv[]){
int k;
FILE *fp = fopen(argv[1], "r");
int c;
int wordCcount = 1;
if(fp != NULL){
while((c=fgetc(fp))) {
if(c == EOF) {break;}
if(c == ' '){
wordCcount++;
}
}
printf("There are %d words in the file %s ",wordCcount, argv[1]);
fclose(fp);
}
else{
printf("File does not exist ");
}
return 0;
}
Output:
sh-4.2$ ./main f.txt
There are 2 words in the file f.txt
Question 3:
#include<stdio.h>
#include<stdlib.h>
int main(int argc, char *argv[]){
int k;
FILE *fp = fopen(argv[1], "r");
int c;
int lineCount = 1;
if(fp != NULL){
while((c=fgetc(fp))) {
if(c == EOF) {break;}
if(c == ' '){
lineCount++;
}
}
printf("There are %d lines in the file %s ",lineCount, argv[1]);
fclose(fp);
}
else{
printf("File does not exist ");
}
return 0;
}
Output:
sh-4.2$ ./main f.txt
There are 2 lines in the file f.txt
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.