Thread: Request for feedback -- exercise solution.

  1. #1
    Novice
    Join Date
    Jul 2009
    Posts
    568

    Request for feedback -- exercise solution.

    Hey, folks!

    I'd appreciate if you could review my solution to the following exercise for correctness. I've tested it for several different input and sequential calls, and I couldn't spot any obvious bugs. Any suggestions for improvement are welcome!

    Exercise
    "Design and test a function that reads the first word from a line of input into an array and discards the rest of the line. Define a word as a sequence of characters with no blanks, tabs, or newlines in it." (I've added three features the exercise does not explicitly ask for -- array is always null-terminated, bsize (buffer size) is passed as an argument to the function, and function returns the number of characters read.)

    My solution
    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    int msh_getw(char* buffer, int bsize) {
      int cnt = 0;
      while (cnt < bsize) {
        *buffer = getchar();
        if ( isspace(*buffer)) {
          *buffer = '\0';
          while( getchar() != '\n');
          return cnt;
        }
        cnt++;
        buffer++;
      }
      
      *(buffer - 1) = '\0';
      while( getchar() != '\n');
      return cnt;
    }
    Thanks for you time!

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Looks good, at least to me.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    You are performing the input retrieval within the function. I do not believe that to be the intent of the exercise. For example, what if you want the first word of a line read from a file, rather than a line read directly from the user?

    Also, comments in your function would be a nice addition.

  4. #4
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    *buffer = getchar();
    getchar() returns EOF and ...
    Does it handle empty input line properly?

  5. #5
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Quote Originally Posted by Bayint Naung View Post
    *buffer = getchar();
    getchar() returns EOF and ...
    Does it handle empty input line properly?
    Oh yeah...

    I'll work on those.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 05-18-2010, 04:14 AM
  2. Function call from another .c module
    By Ali.B in forum C Programming
    Replies: 14
    Last Post: 08-03-2009, 11:45 AM
  3. Line of data input method
    By larry_2k4 in forum C Programming
    Replies: 2
    Last Post: 04-28-2009, 11:34 PM
  4. Can you check what is wrong with this code
    By Ron in forum C++ Programming
    Replies: 4
    Last Post: 08-01-2008, 10:59 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM