Sponsored Content
Top Forums Programming Bug in "Word Counting" Program Post 302620031 by sreeharshasn on Friday 6th of April 2012 08:50:54 PM
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?
 

8 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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
vlatai(1L)																vlatai(1L)

NAME
vlatai - Lojban word analyzer SYNOPSIS
vlatai [ -v ] [ word ] DESCRIPTION
vlatai is a program that reads a list of Lojban words from standard input, one word per line. In its usual mode, it writes a single output line for each input line, advising of which kind of Lojban word the input line is. If the input word has cmavo prefixed onto it, vlatai shows the individual cmavo and the base word itself. In the verbose mode (obtained using -v), the workings of the internal state machines used for scanning the word are exposed. This may give some insight into where an error lies on words that can't be parsed, especially if the output is used in conjuction with the report files built during the compilation process. Note that vlatai is primarily a testbench for the word categorizer/splitter inside jbofihe. vlatai is not fully supported or documented as a stand-alone program. OPTIONS
-v Verbose; show lots of information about the evolution of the internal state word The word to check. If no 'word' argument is supplied, the program reads words from standard input, one word per line (with no whitespace around it.) REFERENCES
http://go.to/jbofihe Home page for the jbofihe project (of which cmafihe is part) http://www.lojban.org/ Home page of the Lojban community http://www.rrbcurnow.freeuk.com/lojban/ My Lojban page. AUTHOR
Richard Curnow <rpc@myself.com> December 2000 vlatai(1L)
All times are GMT -4. The time now is 01:49 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy