Reputation:
Here is a simple count word program, which i feel is very efficient. Is this the best way to count words in C, or are there any flaws in this program?
#include <stdio.h>
int CountWords(void);
main()
{
printf("count the words and enter string\n");
CountWords();
}
int CountWords(void)
{
char c;
int num=0;
int flag= 0;
while((c=getchar())!='\n')
{
if(c==' ')
{
flag=0 ;
}
else if(flag==0)
{
num++;
flag=1;
}
}
printf("Num is %d",num);
}
Upvotes: 0
Views: 11835
Reputation: 1
Here is the full piece of code that can improve that program and which works in all conditions:
#include <stdio.h>
void CountWords(void);
void main()
{
printf("\n\tcount the words and enter string\n\n\n");
CountWords();
}
void CountWords(void)
{
char c;
int num=0;
int flag= 0;
while((c=getchar())!='\n')
{
if((c==' ')||(c==' ')||(c=='.')||(c==';')||(c==',')||(c==';')
||(c==':')||(c=='"')||(c=='?')||(c=='!')||(c=='-'))
{
flag=0 ;
}
else if(flag==0)
{
num++;
flag=1;
}
}
printf("\t\n\n\nNumber of words is %d\n",num);
}
/*By Md. azaz*/
Upvotes: 0
Reputation: 1
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define SIZE 100
int main()
{
char str[SIZE]={ '\0' };
int string_size;
int wordcount, found=0, other=0;
printf("Enter the string:");
fgets(str, SIZE, stdin);
string_size = strlen(str);
for(int i=0;i<=string_size;i++)
{
if(isalnum(str[i]) && !isalnum(str[i-1]))
found++;
}
printf("Number of words are:%d\n", found);
return 0;
}
Upvotes: 0
Reputation: 11162
bah. Way too much code:
int numwords(char *str)
{ int n = 0;
for(str=strtok(str, " -.!,;"); str; str=strtok(NULL, " -.!,;"))
n++;
return n;
}
is a blatant way to count words in c. libc strtok()
is highly optimized.
Upvotes: 0
Reputation: 13964
Define your problem in terms of an algorithm first. For example, make a flow chart. Run some quick examples through your logic, just as other posters have done. Do all this before you write code.
Then, once you think you have the best algorithm, write that in C.
Come up with a list of questions to ask yourself, such as "What is a word?", "What is not a word?".
For parsing text, or tokens of any kind, you may be interested in expressing your ideas in terms of Backus-Naur Form. Take a look at any computer language specification, and notice how they define something like an identifier. Even if you don't use this to write your program, it should help you think out the problem.
Are words restricted to alphabetic characters a-z and A-Z? What about hyphenated words?
Perhaps (not in formal BNF):
hyphen := '-'
alpha := 'a' | 'b' | ... | 'z' | 'A' | 'B' | ... | 'Z'
alphagroup := alpha | alpha alphagroup
hyphenword := alphagroup hyphen alphagroup | alphagroup hyphen hyphenword
word := alphagroup | hyphenword
Upvotes: 0
Reputation: 2576
One thing I can think of is that it will count punctuation marks as words if they are surrounded by spaces.
E.g.
The quick - and lazy - fox.
will be reported as containing 7 words although there are only 5.
You may want to reduce the set of word characters to alphanumeric characters only, and counting punctuation marks as word separators - unless it is a ' or a - in the middle of a word, depending on what you define as a word (are it's and quick-witted single words ? Two words each ?).
Upvotes: 2
Reputation: 11529
Your count will be off by one with this program. This is because you are checking for a space to add one to your count of words. What do you think will happen with the last word in the string?
Upvotes: 3