Go Back   The UNIX and Linux Forums > Top Forums > Programming


Programming Post questions about C, C++, Java, SQL, and other programming languages here.

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 04-06-2012
Registered User
 
Join Date: Apr 2010
Location: India, Bangalore
Posts: 16
Thanks: 6
Thanked 0 Times in 0 Posts
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  
Old 04-06-2012
Registered User
 
Join Date: Jul 2008
Location: NY
Posts: 203
Thanks: 13
Thanked 39 Times in 38 Posts
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  
Old 04-06-2012
Mead Rotor
 
Join Date: Aug 2005
Location: Saskatchewan
Posts: 16,400
Thanks: 492
Thanked 2,537 Times in 2,420 Posts
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  
Old 04-06-2012
Scrutinizer's Avatar
Moderator
 
Join Date: Nov 2008
Location: Amsterdam
Posts: 7,352
Thanks: 144
Thanked 1,756 Times in 1,593 Posts

Code:
if (( ichar != ' ' ) && ( ichar != '\n' ) && ( ichar != '\t' ) && ( in_word == 1 ))

Sponsored Links
    #5  
Old 04-28-2012
Registered User
 
Join Date: Apr 2012
Location: 127.0.0.1
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by Scrutinizer View Post
Code:
if (( ichar != ' ' ) && ( ichar != '\n' ) && ( ichar != '\t' ) && ( in_word == 1 ))

This looks right to me. I'm curious if this was successful?
Sponsored Links
Closed Thread

Tags
word counting

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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



All times are GMT -4. The time now is 08:53 AM.