0

I am new to C programming and I am currently trying to teach myself how to create a C program that can count words and lines in the input stream and print the two totals to the standard output.

What I am trying to do is to have the program count the number of lines and count the number of words in an input stream. I would like to have the program include words but to exclude blanks, tabs, newlines, hyphens, or colons. While having the program output the results (words and lines) as decimals.

#include<stdio.h>

int main()
{
int iochar;
int words;
int lines;

printf("Enter something here:\n\n");

while ((iochar = getchar ()) !=EOF)
    {
    if((iochar == ' ') || (iochar == '\t') || (iochar == '\n'))

    putchar(iochar);
    }

return 0;
}

I would like to have the program output decimal of the value of words and lines it counted in standard output. This does not seem to be working for me.

Rob Kennedy
  • 161,384
  • 21
  • 275
  • 467
user1698560
  • 1
  • 3
  • 6

4 Answers4

1

You have to increment the value of lines when the read value is \n. To count the number of words, you can see these solutions.

You can also use wc program (UNIX)...

Community
  • 1
  • 1
md5
  • 23,373
  • 3
  • 44
  • 93
1

Try using a switch statement instead of an if, and add some counting logic:

int wordLen = 0;
while (...) {
    switch(iochar) {
    case '\n':
        lines++; // no "break" here is intentional
    case '\t':
    case ' ':
        words += (wordLen != 0);
        wordLen = 0;
        break;
    default:
        wordLen++;
        break;
    }
}
if (wordLen) words++;

There is a K&R chapter that goes through this exercise in details, see section 1.5.4 Word Counting.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
0

You'll want to read up on the standard library functions isspace and ispunct; that's easier than doing explicit tests against various character values (and it takes locale into account).

You'll need to initialize words and lines to 0 and then update them as you check your inputs:

if (isspace(iochar) || ispunct(iochar) || iochar == EOF)
{
  if (previous_character_was_not_space_or_punctuation)  // you'll have to figure
  {                                                     // out how to keep track 
    words++;                                            // of that on your own
  }

  if (iochar == '\n')
  {
    lines++;
  }
}
John Bode
  • 119,563
  • 19
  • 122
  • 198
0

As AK4749 mentioned, you do not have any counting code.

Also in the if statement, you only output the character to stdout if it is space, tab or new line. I believe you want the opposite.

I would try something like the following:

#include "stdio.h"

int main()
{
    int iochar, words,lines;
    words=0;
    lines=0;


    printf("Enter something here:\n\n");

    while ((iochar = getchar ()) !=EOF)
    {
        if((iochar == ' ') || (iochar=='\t')) 
            words++;
        else if (iochar == '\n')
            lines++;
        else
        {
            putchar(iochar);
        }
    }
    printf("Lines: %d, Words: %d", lines, words);
    return 0;
}

I have not tried to compile this but it should not be too far from ok.

Hope it helps, Lefteris

Lefteris
  • 873
  • 2
  • 8
  • 29
  • Thanks, i tried to compile the program but it seems that it might have come back with some error: – user1698560 Sep 26 '12 at 20:46
  • line 10: syntax error before or at: int "program.c", line 27: warning: function prototype parameters must have types "program.c", line 27: warning: old-style declaration or incorrect type for: printf "program.c", line 27: warning: identifier redeclared: printf current : function() returning int previous: function(pointer to const char, ...) returning int : "/usr/include/iso/stdio_iso.h", line 206 "program.c", line 28: syntax error before or at: return "program.c", line 28: warning: syntax error: empty declaration – user1698560 Sep 26 '12 at 20:48
  • Hello, I have updated the code block. I also included the following comment in the update: initializing `words` means that we cannot declare another variable (in C as opposed to C++ you can only declare variables in the beginning of the function or block). Also the test for new line was incorrect (it was reverse). I have updated the code and tried compiling it without problems. – Lefteris Sep 27 '12 at 17:30