Question

In: Computer Science

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.

Solutions

Expert Solution

`Hey,

Note: Brother if you have any queries related the answer please do comment. I would be very happy to resolve all your queries.

#include<stdio.h>
#include<stdlib.h>
int comment1(FILE *fp)
{
char ch;
int count=0;
while(fscanf(fp,"%c",&ch)!=EOF)
{
if(ch=='\n')
{
return count;
}
count++;
}
return count;
}
int comment2(FILE *fp)
{
char ch;
int count=0;
while(fscanf(fp,"%c",&ch)!=EOF)
{
if(ch=='*')
{
fscanf(fp,"%c",&ch);
if(ch=='/')
{
return count;
}
count++;
}
count++;
}
return 0;
}
int countWords(char s[])
{
int i,count=0;
for (i = 0;s[i] != '\0';i++)
{
if (s[i] == ' ' && s[i+1] != ' ')
count++;
}
return count;
}
int comment1s(FILE *fp)
{
char ch;
int count=0;
char s[10000];
int ct=0;
while(fscanf(fp,"%c",&ch)!=EOF)
{
if(ch=='\n')
{
s[ct]='\0';
return countWords(s);
}
s[ct++]=ch;
count++;
}
s[ct]='\0';
return countWords(s);
}
int comment2s(FILE *fp)
{
char ch;
int count=0;
char s[10000];
int ct=0;
while(fscanf(fp,"%c",&ch)!=EOF)
{
if(ch=='*')
{
fscanf(fp,"%c",&ch);
if(ch=='/')
{
s[ct]='\0';

return countWords(s);
}
s[ct++]='*';
}
s[ct++]=ch;
count++;
}
return 0;
}

int main()
{
printf("Enter the file name:");
char s[1000],ch,ch1;
scanf("%s",s);
{

FILE*fp;
fp = fopen(s,"r");
int count=0;
while(fscanf(fp,"%c",&ch)!=EOF)
{
if(ch=='\"')
{
while(fscanf(fp,"%c",&ch)!=EOF)
{
if(ch=='\"')
{
break;
}
if(ch=='\\')
{
fscanf(fp,"%c",&ch);
}
}

}
else if(ch=='/')
{
fscanf(fp,"%c",&ch);
if(ch=='/')
{
count += comment1(fp);
}
else if(ch=='*')
{
count += comment2(fp);
}
}
}
printf("Characters: %d\n",count);
fclose(fp);
}
{

FILE*fp;
fp = fopen(s,"r");
int count=0;
while(fscanf(fp,"%c",&ch)!=EOF)
{
if(ch=='\"')
{
while(fscanf(fp,"%c",&ch)!=EOF)
{
if(ch=='\"')
{
break;
}
if(ch=='\\')
{
fscanf(fp,"%c",&ch);
}
}

}
else if(ch=='/')
{
fscanf(fp,"%c",&ch);
if(ch=='/')
{
count += comment1s(fp);
}
else if(ch=='*')
{
count += comment2s(fp);
}
}
}
printf("Words: %d\n",count);
fclose(fp);
}

return 0;
}

Kindly revert for any queries

Thanks.


Related Solutions

In Java: Write a program that will count the number of characters, words, and lines in...
In Java: Write a program that will count the number of characters, words, and lines in a file. Words are separated by whitespace characters. The file name should be passed as a command-line argument, as shown below. c:\exercise>java Exercise12_13 Loan.java File loan.java has 1919 characters 210 words 71 lines c:\exercise> Class Name: Exercise12_13
C LANGUAGE ONLY Write a C program to count the total number of duplicate elements in...
C LANGUAGE ONLY Write a C program to count the total number of duplicate elements in an array. Enter the number of elements to be stored in the array: 3 Input 3 elements in the arrangement: element [0]: 5 element [1]: 1 element [2]: 1 Expected output: The total number of duplicate elements found in the array is: 1
2.c++ if and loop statement Write a program that will count the number of even number...
2.c++ if and loop statement Write a program that will count the number of even number and odd numbers between two inputted numbers. Display the numbers and compute the sum and average of all the even numbers and the sum and average all the odd numbers. Sample outputs: Enter starting number:3 Enter starting number:4 Enter ending number:10 Enter ending number:10 odd numbers Even number 3 4 5 6 7 8 9 10 number of even numbers=4 number of even numbers=4...
In C++ For this assignment, you will write a program to count the number of times...
In C++ For this assignment, you will write a program to count the number of times the words in an input text file occur. The WordCount Structure Define a C++ struct called WordCount that contains the following data members: An array of 31 characters named word An integer named count Functions Write the following functions: int main(int argc, char* argv[]) This function should declare an array of 200 WordCount objects and an integer numWords to track the number of array...
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...
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 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 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...
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.
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT