C program to mimic wc command in UNIX

  • Thread starter Thread starter Gagan A
  • Start date Start date
  • Tags Tags
    Program Unix
Click For 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
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 4 ·
Replies
4
Views
1K
Replies
89
Views
6K
  • · Replies 6 ·
Replies
6
Views
3K
Replies
15
Views
5K
  • · Replies 89 ·
3
Replies
89
Views
6K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
73
Views
6K
Replies
7
Views
2K
  • · Replies 32 ·
2
Replies
32
Views
3K
Replies
5
Views
2K