Like Tree1Likes
  • 1 Post By std10093

C Program that Counts words like a Unix word count

This is a discussion on C Program that Counts words like a 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

    Unhappy C Program that Counts words like a 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
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    C question.Must be moved by the admins of the forum in the C thread.

    Meanwhile you can take a look in scanf function
    laserlight likes this.

  3. #3
    Registered User
    Join Date
    Sep 2012
    Posts
    6
    Okay thank you!

  4. #4
    qny
    qny is offline
    Registered User
    Join Date
    Sep 2012
    Posts
    355
    I'd do with
    a) fgets()
    b) a user-defined function to count words in a line

    fgets() reads lines from the input, so you just need to put it in a loop and count how many times the loop is executed: that's the number of lines (check for miscalculations at the end of the input)

    the user-defined function can be a very simple finite state automaton.

  5. #5
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    getchar i would say is what the professor wants them to use it,but if you read my most,this a post that has been re-opened in the C thread,so write there please any suggestions that you might have

    EDIT-> Oops,we are in C thread,sorry gny!But the new guy has opened the post again and i gave an answer there
    Last edited by std10093; 09-21-2012 at 04:14 AM.

  6. #6
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,157
    Did you miss the part of the assignment that states:
    Quote Originally Posted by NewGuy2CProg View Post
    ... that will read data from standard input one character at a time...
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault( counts number of words program )
    By paranoidgnu in forum C Programming
    Replies: 22
    Last Post: 05-12-2011, 04:08 PM
  2. Replies: 1
    Last Post: 04-27-2011, 10:56 PM
  3. c program to count words
    By bigwillys111 in forum C Programming
    Replies: 3
    Last Post: 04-22-2011, 11:57 PM
  4. count words program problem
    By Tony654321 in forum C Programming
    Replies: 8
    Last Post: 10-19-2004, 11:23 AM
  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