Bug in "Word Counting" Program


 
Thread Tools Search this Thread
Top Forums Programming Bug in "Word Counting" Program
# 1  
Old 04-06-2012
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?
# 2  
Old 04-06-2012
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 10:21 PM.. Reason: changed inword definition
# 3  
Old 04-06-2012
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
Code:
if (( ichar != ' ' ) && ( ichar != '\n' ) && ( ichar != '\t' ) && ( in_word == 1 ))

# 5  
Old 04-28-2012
Quote:
Originally Posted by Scrutinizer
Code:
if (( ichar != ' ' ) && ( ichar != '\n' ) && ( ichar != '\t' ) && ( in_word == 1 ))

This looks right to me. I'm curious if this was successful?
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

2. Shell Programming and Scripting

How do i replace a word ending with "key" using awk excpet for one word?

echo {mbr_key,grp_key,dep_key,abc,xyz,aaa,ccc} | awk 'gsub(/^|abc,|$/,"") {print}' Required output {grp_key,xyz,aaa,ccc} (5 Replies)
Discussion started by: 100bees
5 Replies

3. Shell Programming and Scripting

Please identify "unexpected end of file" bug

This is a script I got off the web for transferring Safari's "reading list" to the Pocket app. I ran it in terminal with the command bash exportsafarireadinglist.sh and got syntax error: unexpected end of file. Thanks for any help! The code: #!/bin/bash # Script to export Safari's reading... (2 Replies)
Discussion started by: kdog126
2 Replies

4. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

5. Cybersecurity

Tor Browser Bundle for Linux (2.2.35-8) "EVIL bug"

There is an EVIL bug in at least the Linux (2.2.35-8) Tor Browser Bundle start-tor-browser script. It will log things like domain names to a file in the root of the browser bundle. trac.torproject.org/projects/tor/ticket/5417 Ticket #5417 (new defect) RelativeLink.sh in Tor browser bundle... (0 Replies)
Discussion started by: madeinindia
0 Replies

6. UNIX for Dummies Questions & Answers

Counting vowels in string. "Comparison pointer-integer".

I'm trying to write a programme which scans strings to find how many vowels they contain. I get an error saying that I'm trying to compare a pointer and an integer inif(*v == scanme){. How can I overcome this ? Also, the programme seems to scan only the first word of a string e.g.: if I type "abc... (1 Reply)
Discussion started by: fakuse
1 Replies

7. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

8. Programming

how could i make a program mixed with many "|", "<" and ">"

I have written following code to do: ls -l | wc -w, it works: but when there are not only a single "|", if there are more such as: ls -l | sort -r | sort | sort -r, This program does not work, i want to know how could i deal with it when there are more "|", another situation is that, if it mixes... (2 Replies)
Discussion started by: strugglingman
2 Replies
Login or Register to Ask a Question