Question

In: Computer Science

Write a C program that counts the number of non white-space characters in an inputtext file....

Write a C program that counts the number of non white-space characters in an inputtext file. Program takes as command argument(s)the name of the input file (and the output file with option -f). Based on the option flags, it displays the output on the standard output, or writesthe output to an output file.

The command format is as follows:

command -f inputfile outputfile

or,

command -s inputfile

-f indicates writing to an output file;

-s indicates displaying the output on the screen.

Also notice the errors caused by incomplete arguments.

Solutions

Expert Solution

#include<stdio.h>
#include<stdlib.h>
int main(int argc, char ** argv)
{
FILE *file=NULL;char ch;
int count_char=0;
file=fopen(argv[2],"r");
if(!file){
printf("error while opening the file\n");exit(0);}
while( (ch = getc(file))!= EOF)
{

   if(ch!=' ')
count_char++;
}
fclose(file);
if(argv[1][0]=='s')
{
printf("Total number of non space characters in a file is = %d\n",count_char);
}
else if(argv[1][0]=='f')
{
FILE * file1;
file1=fopen(argv[3],"w");
if(file1){
fprintf(file1,"%s %d", "Total number of non space characters in a file is =", count_char);
fclose(file1);}
else
printf("unable to write into file\n");
}
}

  


Related Solutions

Write a C program that counts the number of repeated characters in a phrase entered by...
Write a C program that counts the number of repeated characters in a phrase entered by the user and prints them. If none of the characters are repeated, then print “No character is repeated” For example: If the phrase is “full proof” then the output will be Number of characters repeated: 3 Characters repeated: f, l, o Note: Assume the length of the string is 10. ###Note: the output should print exactly as it is stated in the example if...
Create a program in C that counts the number of characters in a word when a...
Create a program in C that counts the number of characters in a word when a user inputs a string. Use stdin to read an input string. For example, if a user inputs: “The dog is good” the output should be a= [The], b=3 a= [dog], b=3 a= [ is], b=2 a= [good], b=4 a= [ ], b=0000 Take into account EOF. If an EOF is reached at the end of the string then the output should be 0000. (example...
Create a program in C that counts the number of characters in a word when a...
Create a program in C that counts the number of characters in a word when a user inputs a string. Use stdin to read an input string. For example, if a user inputs: “The dog is good” the output should be a= [The], b=3 a= [dog], b=3 a= [ is], b=2 a= [good], b=4 a= [ ], b=0000 Take into account EOF. If an EOF is reached at the end of the string then the output should be 0000. (example...
I need a C++ program using while loops that counts the number of characters in a...
I need a C++ program using while loops that counts the number of characters in a sentence. The user inputs a sentence and then terminates the input with either '.' or '!'. And then it needs to count and display the number of a's, e's, i's, o's, u's, and consonants. The program should read both lower and upper case. They don't want us using switch statements or string operators, and want us to us if else if statements. I have...
write this program in C++ Write a program that prompts a user for three characters. The...
write this program in C++ Write a program that prompts a user for three characters. The program must make sure that the input is a number 10 - 100 inclusive. The program must re prompt the user until a correct input is entered. Finally output the largest and the lowest value. Example 1: Input : 10 Input : 20 Input : 30 The largest is 30. The lowest is 10. Example 2: Input : 100 Input : 50 Input :...
Write a C program that counts the number of odd numbers with using function count() within...
Write a C program that counts the number of odd numbers with using function count() within the set. The set has only one negative number which determines the end of set.
2. [50] Write a C program to count the total number of commented characters and words...
2. [50] Write a C program to count the total number of commented characters and words in a C file taking both types of C file comments (single line and block) into account.
Write a program that reads in characters until end of file. The program should count and...
Write a program that reads in characters until end of file. The program should count and print the number of characters, printable characters, vowels, digits, and consonants in the input. Use functions to check whether a character is a vowel, a consonant, or a printable character. Define and use macros to test if a character is a digit or a letter.
Write a C program to find out the number of words in an input text file...
Write a C program to find out the number of words in an input text file (in.txt). Also, make a copy of the input file. Solve in C programming.
write a program in C Write a function that is passed an array of characters containing...
write a program in C Write a function that is passed an array of characters containing letter grades of A, B, C, D, and F, and returns the total number of occurrences of each letter grade. Your function should accept both lower and upper case grades, for example, both 'b' and 'B' should be bucketed into your running total for B grades. Any grade that is invalid should be bucketed as a grade of 'I' for Incomplete. You must use...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT