C program to mimic wc command in UNIX

In summary, the conversation is about a flawed algorithm for counting the number of characters, lines, and words in a file. The person suggests using the isspace() function declared in ctype.h to include other whitespace characters as tokens that delimit a word. They also point out a test case that the algorithm does not consider correctly.
  • #1
Gagan A
20
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
  • #2
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
 
  • #3
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
 

1. What is the purpose of a C program to mimic wc command in UNIX?

A C program to mimic wc command in UNIX is used to count the number of lines, words, and characters in a given file or input. It is a useful tool for analyzing and manipulating text files in UNIX systems.

2. How does a C program to mimic wc command in UNIX work?

The program uses various functions and loops to read and count the characters, words, and lines in the given file. It also takes into account special cases such as multiple spaces between words and empty lines.

3. Can a C program to mimic wc command in UNIX handle multiple files?

Yes, the program can be modified to handle multiple files by using command line arguments or by prompting the user for file names. It can also provide a total count for all the files combined.

4. Is a C program to mimic wc command in UNIX platform-specific?

No, the program can be written to work on any platform as long as the necessary C libraries are available. However, there may be slight variations in the command line options and arguments based on the specific operating system.

5. What are the advantages of using a C program to mimic wc command in UNIX?

Using a C program offers more flexibility and customization options compared to the built-in wc command in UNIX. It also allows for better understanding and learning of programming concepts and can be easily modified for specific needs.

Similar threads

  • Programming and Computer Science
Replies
4
Views
733
  • Programming and Computer Science
3
Replies
89
Views
4K
  • Programming and Computer Science
3
Replies
89
Views
4K
  • Programming and Computer Science
Replies
14
Views
2K
  • Programming and Computer Science
Replies
7
Views
1K
  • Programming and Computer Science
3
Replies
73
Views
4K
  • Programming and Computer Science
Replies
32
Views
2K
  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
Replies
30
Views
2K
  • Programming and Computer Science
Replies
5
Views
879
Back
Top