Question

What are the description of approach or technique used in this? #include <stdio.h> long charCount=0; long...

What are the description of approach or technique used in this?

#include <stdio.h>

long charCount=0;

long digitCount=0;

long lineCount=0;

long wordCount=0;

long digitFreq[10];

int c=0;

int outOfWord=0;

int state=0;

int inWord=1;

void printStats();

void totalWords();

void totalLines();

void digitFrequency();

int main(int argc,char **argv)

{

int i;

for(i=0;i<10;i++)

{

digitFreq[i]=0;

}

state=outOfWord;

c=getchar();

while((c !=EOF))

{

charCount++;

digitFrequency(c);

totalLines(c);

totalWords(c);

c=getchar();

}

printStats();

return 1;

}

void totalLines(int c)

{

if(c == '\n')

{

lineCount++;

}

}

void digitFrequency(int c)

{

if(c >='0'&& c<='9')

{

digitCount++;

digitFreq[c-'0']++;

}

}

void totalWords(int c)

{

if(c == ' ' || c == '\n' || c == '\t')

{

state=outOfWord;

}

else if(state == outOfWord)

{

state = inWord;

wordCount++;

}

}

void printStats()

{

printf("words: %ld\nchars: %ld\nlines: %ld\n",wordCount,charCount,lineCount);

int i;

for(i=0;i<10;i++)

{

printf("digit %d: %ld\n",i,digitFreq[i]);

}

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The porgram is about counting the number of characters,no of digits,no of lines,no of words and frquency of digits.

charCount is used to store no of characters

digitCount is used to store no of digits

lineCount is used to store no of lines

word count is used to store no of words

array digitFreq of size 10 is used to frquency of digits in the file.

OutOfWord and inWord is used to indicate that reading of one word completed.

Initially we didnt know howmany digits are there and what are their frequencies ofdigits so initializing that array with zero in all indexes

after that we are reading character by character form the file into c using getchar function

we will continue reading till the end of file using while loop.

Inside the while loop we have code to count statistics

Every thing we read is a character thats why first statement in while is incrementing teh character count

second statement we are calling digitFrequency(c)

in this function we are cheking whether teh character we red was in range o and 9 if it is then we are incrementing value in that particular index in that array

Third statement is calling function totalLines(c) to check whether the character we read was end of line or not.

in this function we are checking whether c is "\n" or not which indicates teh end of line if yes incrementing lineCount

fourth statement we are calling totalWords(c) to check whether we have completed reading a word or

not

a word will end if the character we read was a space or tab(\t) or \n(end of line) if it is then incrementing word count.

and fifth statement id again reding chharacter from file and again loop repesta till end of file.

The technique here used is simple one. We are counting the statistics of a file by using simple basic things.

Example every thing in file is a character so incrementing charCount for every thing we read from file

and a line ends with \n so if the character we read is \n then it indicates that we have finished reading a line so incrementing line count

a word will end if the character we read was a space or tab(\t) or \n(end of line) if it is then incrementing word count.

and final thing is counting the digits. Any number can be formed by numbers between 0 t0 9 . This the thing we are using in this code, if the character read from file is

in range 0 to 9 and we are incrementing that digit frequency and storing that in array

Add a comment
Know the answer?
Add Answer to:
What are the description of approach or technique used in this? #include <stdio.h> long charCount=0; long...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Counting characters, words, and lines based on a text file

    I did the assigment but inside mainWrite a C program (called counting.c) that counts the number of characters, words and lines readfrom standard input (stdin) until EOF is reached. This means counting.c must contain a mainfunction along with other function.- Assume the input is ASCII text of any length.-Every byte read from stdin counts as a character except EOF.- Words are defined as contiguous sequences of letters (a through z, A through Z) and the apostrophe ( ' which has...

  • c program that counts the number of characters, words and lines from standard input until EOF....

    c program that counts the number of characters, words and lines from standard input until EOF. attached is what i Have so far but its not working ?. about shell redirection Requirements 1. Write a C program that counts the number of characters, words and lines read from standard Input until EOF Is reached. 2. Assume the Input is ASCII text of any length. 3. Every byte read from stdin counts as a character except EOF 4. Words are defined...

  • include «stdio.h void displaymenu(void) printf(" printf" Enter Choicen") printf Set (a), Clear (c), Toggle (t)\n"): printf("...

    include «stdio.h void displaymenu(void) printf(" printf" Enter Choicen") printf Set (a), Clear (c), Toggle (t)\n"): printf(" void printbinaryfunsigned char x) int main (int argc, char argvl) r mask atoi(argv[1): unsigned char maskatoi(argv[11): ensigned ahao data - ateilargvl21): char operation if argc 3 printf(" printf" n" printf("usage: %s FristArg SecondArgin", argle)); return;

  • #include <stdio.h> int main(int argc, char *argv[]) { int i; for (i = argc - 1;...

    #include <stdio.h> int main(int argc, char *argv[]) { int i; for (i = argc - 1; i > 0; i--) printf("%s ", argv[i]); printf("\n"); return 0; } can you explain this code in c and why use this function  

  • Would u help me fixing this CODE With Debugging Code with GDB #include <stdio.h> #include <stdlib.h>...

    Would u help me fixing this CODE With Debugging Code with GDB #include <stdio.h> #include <stdlib.h> #define SIZE (10) typedef struct _debugLab { int i; char c; } debugLab; // Prototypes void PrintUsage(char *); void DebugOption1(void); void DebugOption2(void); int main(int argc, char **argv) { int option = 0; if (argc == 1) { PrintUsage(argv[0]); exit(0); } option = atoi(argv[1]); if (option == 1) { DebugOption1(); } else if (option == 2) { DebugOption2(); } else { PrintUsage(argv[0]); exit(0); } }...

  • Combine two codes (code 1) to get names with(code 2) to get info: Code 1: #include<unistd.h>...

    Combine two codes (code 1) to get names with(code 2) to get info: Code 1: #include<unistd.h> #include<sys/types.h> #include<sys/stat.h> #include<fcntl.h> #include<dirent.h> #include<stdio.h> #include<stdlib.h> void do_ls(char []); int main(int argc,char *argv[]) { if(argc == 1) do_ls("."); else while(--argc){ printf("%s:\n",*++argv); do_ls(*argv); } } void do_ls(char dirname[]) { DIR *dir_ptr; struct dirent *direntp; if((dir_ptr = opendir(dirname)) == NULL) fprintf(stderr,"ls1:cannot open %s\n",dirname); else { while((direntp = readdir(dir_ptr)) != NULL) printf("%s\n",direntp->d_name); closedir(dir_ptr); } } ____________________________ code 2: #include <stdio.h> #include <sys/types.h> #include <sys/stat.h> void show_stat_info(char *,...

  • #include <stdio.h> #include <stdlib.h> #include <string.h> #include<ctype.h> #define MAX_LEN 255 int numWords(char *str); int numDigit(char *str);...

    #include <stdio.h> #include <stdlib.h> #include <string.h> #include<ctype.h> #define MAX_LEN 255 int numWords(char *str); int numDigit(char *str); int numUppltr(char *str); int numLwrltr(char *str); int punChar(char *str); char*readString(char *str); int main() { char givString[MAX_LEN]; puts("Enter your string(Max 255 characters):"); //readString(givString); gets(givString); printf("\nnumber of words :%d",numWords(givString)); printf("\nnumber of uppercase letters %d",numUppltr(givString)); printf("\nnumber of lowercase letters %d",numLwrltr(givString)); printf("\nnumber of punctuations %d\n",punChar(givString)); printf("\nnumber of digits:%d\n",numDigit(givString)); system("pause"); return 0; } char *readString(char *str) { int ch, i=0; while((ch=getchar())!=EOF && ch!='\n') { if(i) { str[i]=ch; i++; }...

  • 1 #include <stdio.h> 2 3 11 Remember this written by the 4 // "new guy down...

    1 #include <stdio.h> 2 3 11 Remember this written by the 4 // "new guy down the hall". 5 6 pint main(int argc, char *argv[]) { 7 int a = 200; 8 int b = 300; 9 int c = 500; 10 if (a > 400) 11 b += 10; 12 else 13 if (c > 300) b += 50; 14 else b += 20; C += 10; 15 C += 100; 16 if (b > 800) 17 C +=...

  • OPERATING SYSTWM Question 31 What is the output of this C program? #include #include void main()...

    OPERATING SYSTWM Question 31 What is the output of this C program? #include #include void main() int mptr, *cptr mptr = (int*)malloc(sizeof(int)); printf("%d", "mptr); cptr = (int)calloc(sizeof(int),1); printf("%d","cptr); garbage 0 000 O garbage segmentation fault Question 8 1 pts If this program "Hello" is run from the command line as "Hello 12 3" what is the output? (char '1'is ascii value 49. char '2' is ascii value 50 char'3' is ascii value 51): #include<stdio.h> int main (int argc, char*argv[]) int...

  • Convert C to C++ I need these 4 C file code convert to C++. Please Convert...

    Convert C to C++ I need these 4 C file code convert to C++. Please Convert it to C++ //////first C file: Wunzip.c #include int main(int argc, char* argv[]) { if(argc ==1){ printf("wunzip: file1 [file2 ...]\n"); return 1; } else{ for(int i =1; i< argc;i++){ int num=-1; int numout=-1; int c; int c1;    FILE* file = fopen(argv[i],"rb"); if(file == NULL){ printf("Cannot Open File\n"); return 1; } else{ while(numout != 0){    numout = fread(&num, sizeof(int), 1, file);    c...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT