|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| Programming Post questions about C, C++, Java, SQL, and other programming languages here. |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Bug in "Word Counting" Program
I have written a simple program that counts the number of words in the input stream. There is a small bug in the code and i am not able to figure out the cause of this bug. Code:
#include <stdio.h>
int main()
{
int ichar = 0;
int in_word = 1;
// in_word = 1 *outside a word* in_word = 0 *inside a word*
int wc = 0;
while((ichar = getchar()) != EOF )
{
if ((( ichar != ' ' )||(ichar != '\n' )||(ichar !='\t')) && ( in_word == 1 ))
{
in_word = 0;
wc ++ ;
}
else if (( ichar == ' ' )||(ichar == '\n' )||(ichar =='\t'))
{
in_word = 1;
}
}
printf ( "Total number of words %d\n" , wc);
return 0;
}an \n Whenever i give the above input to the program, (the input contains 2 alphabets a,n then followed by a space and enterkey) the output I get Total number of words 2 Can someone help me understand why i am getting the output as 2 words? |
| Sponsored Links | ||
|
|
#2
|
|||
|
|||
|
Your code is basically counting characters and not words; try this, I've not tested it, but should work Code:
#include <stdio.h>
int main()
{
int ichar = 0;
int in_word = 1;
// in_word = 0 *outside a word* in_word = 1 *inside a word*
int wc = 0;
while((ichar = getchar()) != EOF )
{
if (( ichar != ' ' )||(ichar != '\n' )||(ichar !='\t'))
{
in_word = 1;
}
else if ((( ichar == ' ' )||(ichar == '\n' )||(ichar =='\t')) && ( in_word == 1 ))
{
in_word = 0;
wc ++ ;
}
}
printf ( "Total number of words %d\n" , wc);
return 0;
}Last edited by 47shailesh; 04-06-2012 at 09:21 PM.. Reason: changed inword definition |
| Sponsored Links | ||
|
|
#3
|
|||
|
|||
|
Every time a letter is in a word and is not whitespace, you add 1 to wc. That happens twice when you type in ab. I'd use a few loops: Code:
#include <stdio.h>
#include <ctype.h>
int main()
{
int wordcount=0;
while(!feof(stdin))
{
int c;
while(isspace(c=fgetc(stdin));
if(c < 0) break;
wordcount++;
while(!isspace(c=fgetc(stdin)))
{
if(c<0) break;
}
if(c<0) break;
}
printf("wordcount %d\n", wc);
} |
|
#4
|
||||
|
||||
|
Code:
if (( ichar != ' ' ) && ( ichar != '\n' ) && ( ichar != '\t' ) && ( in_word == 1 )) |
| Sponsored Links | |
|
|
#5
|
|||
|
|||
|
This looks right to me. I'm curious if this was successful?
|
| Sponsored Links | ||
|
![]() |
| Tags |
| word counting |
| Thread Tools | Search this Thread |
| Display Modes | |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Counting vowels in string. "Comparison pointer-integer". | fakuse | UNIX for Dummies Questions & Answers | 1 | 12-06-2011 12:22 AM |
| awk command to replace ";" with "|" and ""|" at diferent places in line of file | shis100 | Shell Programming and Scripting | 7 | 03-16-2011 08:59 AM |
| Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`" | Lokesha | UNIX for Dummies Questions & Answers | 4 | 12-20-2007 12:52 AM |
| how could i make a program mixed with many "|", "<" and ">" | strugglingman | Programming | 2 | 04-29-2006 08:11 AM |
|
|