C Program that Counts Words like Unix word count

This is a discussion on C Program that Counts Words like Unix word count within the C Programming forums, part of the General Programming Boards category; I was given an assignment in which i have to write a C program that will read data from standard ...

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    6

    C Program that Counts Words like Unix word count

    I was given an assignment in which i have to write a C program that will read data from standard input one character at a time and output the results to standard output. This program should work somewhat like the Unix word count program wc.

    The program should count the number of words and lines in the input stream, and print these two numbers on the standard output. Each of these numbers should be right-justified in an 8-column field. The (decimal) number representing the number of words should be in the first 8-columns and the number of lines should be in columns 9-16. Counting the number of words depends on the definition of a word. For this assignment, a "word" is a maximal [nonempty] sequence of characters that do not include blanks, tabs, newlines, hyphens (-), nor colons. Thus "one;two" is considered a single word, but "one:two" would count as two words.

    I am use to using java, which we were introduced from the beginning of this course. Sorry if i am asking for much, i am just really panicking because C programming is so new to me.




  2. #2
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,323
    You will get more responses if you post some code, or an algorithm that you wish to use.
    Fact - Beethoven wrote his first symphony in C

  3. #3
    Registered User
    Join Date
    Sep 2012
    Posts
    6
    Oh Sorry, We were given a template to start from:

    /* Include Files */
    #include <stdio.h>


    int main( void )
    {
    int iochar; /* Note 1 */


    while ( ( iochar = getchar() ) != EOF ) /* Note 2 */
    putchar( iochar ); /* Note 3*/
    return 0;

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    Here is the right thread.Also welcome to the forum. I am going to answer partially in only the below and i 'll tell you why
    Code:
    I was given an assignment in which i have to write a C program that will read data from standard input one character at a time and output the results to standard output. This program should work somewhat like the Unix word count program wc.
    
    The program should count the number of words and lines in the input stream, and print these two numbers on the standard output.
    You may use getchar() http://www.google.ch/url?sa=t&rct=j&...WlffGRC_BPUc_Q function into a loop.Check what EOF is too.getchar brings from the input buffer every single character every time you call her.1st call of her returns the 1st character of the input buffer,2nd call of getchar returns the 2nd character of the input and so on...
    You could also check if this returned value is something that is used to seperate words or it is what you hit when you wrote that post and wanted to change line.Let's say i would like to check if the return value is d,then i would write

    Code:
    ...
    char c;
    c=getchar();
    if(c=='d')
    {
        /*it is d */
    }
    now for the rest of the code,you have first to try it by your own,post -of course- your attempt and then get some help

    I highly recommend you to see this example
    Code:
    #include <stdio.h>
    
    int main(void)
    {   
           int ch, i = 0;
           while((ch = getchar()) != EOF)
           {
              i ++;
            }
           printf("%d\n", i);
    }
    notice that here ch ,the returned value is declared as an int,so the End Of File (EOF) can be handled easily.In the reference link above the returned value is declared as char because no EOF handling needed(but int is suggested in any way some people say).Try to figure out what the example does.If not run it.If not again,i am here

  5. #5
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    Quote Originally Posted by NewGuy2CProg View Post
    Oh Sorry, We were given a template to start from:

    /* Include Files */
    #include <stdio.h>


    int main( void )
    {
    int iochar; /* Note 1 */


    while ( ( iochar = getchar() ) != EOF ) /* Note 2 */
    putchar( iochar ); /* Note 3*/
    return 0;
    ClickHere was talking about your attempt,not the teacher's start-up code

  6. #6
    Registered User
    Join Date
    Sep 2012
    Posts
    6
    Sorry, this is my first time ever using a forum, please forgive me. As far as for the example that is highly recommended, i am going to look into it right now. Hope i come back with a program with your wonderful help.

  7. #7
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    Ok do not worry No penalty is going to be given ,of course i am not the administrator,but it is understandable that you are a beginner.You have to understand that by attempting by your own,you get yourself practice and not just copy pasted solution that are partially or not at all completely understandable by you,because this will have no effect in your programming skill in the far future,but only in the near future(solve this hw).On the other hand it is not polite to have the others do your hw

  8. #8
    Registered User
    Join Date
    Sep 2012
    Posts
    6
    Okay, i was not trying to have anyone do my hw but i just need a good reference to go to or a tutor to help understand C programming because my professor is not beginner friendly......at all. He practically through this material at us and now the most os the class is panicking.

  9. #9
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    I did not implied you did it It is ok.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C Program that Counts words like a Unix word count
    By NewGuy2CProg in forum C Programming
    Replies: 5
    Last Post: 09-21-2012, 10:49 AM
  2. Segmentation fault( counts number of words program )
    By paranoidgnu in forum C Programming
    Replies: 22
    Last Post: 05-12-2011, 04:08 PM
  3. Replies: 1
    Last Post: 04-27-2011, 10:56 PM
  4. c program to count words
    By bigwillys111 in forum C Programming
    Replies: 3
    Last Post: 04-22-2011, 11:57 PM
  5. Replies: 5
    Last Post: 09-28-2004, 12:38 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21