C program to mimic wc command in UNIX

  • Thread starter Thread starter Gagan A
  • Start date Start date
  • Tags Tags
    Program Unix
AI Thread Summary
The discussion focuses on a C program intended to count characters, lines, and words in a text file. The initial implementation correctly counts characters and lines but inaccurately counts words, often exceeding the actual count provided by the `wc` command. A key issue identified is the exclusion of other whitespace characters, such as tabs and carriage returns, which should be considered as delimiters for words. The suggestion is to utilize the `isspace()` function from `ctype.h` to improve word counting accuracy. Additionally, a specific test case is highlighted where the program miscounts words due to its current logic, emphasizing the need for adjustments to align with standard word counting practices.
Gagan A
Messages
19
Reaction score
0
I did it the following way. The number of characters and lines are coming out fine, but the words are usually more than the actual given by wc.

#include<stdio.h>
int main()
{
FILE *fp;
int words=0,chars=0,lines=0;
char prev,curr; //prev variable is included to exclude multiple spaces.
fp=fopen("input.txt","r");
while((fscanf(fp,"%c",&curr))!=EOF)
{
chars++;
if (curr=='\n') lines++;
if ((curr==' ' && prev!=' ') || (curr=='\n' && prev!='\n')) words++; //prev variable comes into play here. if the current char is a space and the previous was also a space then it should not be counted.
prev=curr;
}
printf("%d %d %d\n",chars,lines,words);
return 0;
}
 
Last edited:
Technology news on Phys.org
Use isspace() declared in ctype.h

HiHo!

The mistake is that you have not included the other whitespace characters
(e.g., '\t' and '\r') as tokens that delimit a word.
So, instead of (curr==' ' && prev!=' ') || (curr=='\n' && prev!='\n'), you
should use those is*-functions (e.g. isspace()) declared in ctype.h.

Regards,
Eus
 
Flawed algorithm

HiHo!

Oh, one more thing, you have not considered a test case like this one below.
people<SPACE><ENTER>people.
wc will count that as two words but yours will count that as three words.

Regards,
Eus
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
Back
Top