/*=======================================================================*/ #define PROGRAMMER "SOLUTIONS, Nofrills" #define PROG_CODE "SOLN" #define COURSE "ECE-1021" #define YEAR (2004) #define TERM "Fall" #define SECTION (0) #define ASSIGNMENT "HW5" #define REVISION (0) #define TITLE "Getting Strings from the Keyboard" #define SUBTITLE "Last Modified on 06 SEP 04" #define EMAIL "ece1021@eas.uccs.edu" #define FILENAME "5_0SOLN0.c" /*=======================================================================*/ /*=======================================================================*/ /* WAIVED COMPILER WARNINGS */ /*=======================================================================*/ /* Linker Warning: No module definition file specified: using defaults Reason for Waiver: Can't suppress. Does not have adverse impact. */ /*=======================================================================*/ /* NOTES TO THE USER/GRADER */ /*=======================================================================*/ /* For best results, set the SIZE macro to something between 6 and 30 */ /*=======================================================================*/ /* PROBLEM STATEMENT */ /*=======================================================================*/ /* Write the function: int kgets(char *s, size_t size); The function reads characters from the standard input device using getc() until either a newline character is read, an EOF value is returned by getc(), or (size-1) characters have been read. The string is then null terminated. Neither the newline character nor EOF (which is not an ASCII code to begin with) is stored in the string. If the function did not encounter a newline character or EOF before reading in (size-1) characters, the function should continue reading in (and discarding) characters from stdin until a newline character or EOF is read. The function should return the number of characters that were discarded. */ /*=======================================================================*/ /* DEVELOPMENT NOTES */ /*=======================================================================*/ /* */ /*=======================================================================*/ /* PSEUDOCODE */ /*=======================================================================*/ /* */ /*=======================================================================*/ /* DEVIATIONS FROM SUBMITTED PSEUDOCODE */ /*=======================================================================*/ /* */ /*=======================================================================*/ /*=======================================================================*/ /* CODE SECTION */ /*=======================================================================*/ /*=======================================================================*/ /*== INCLUDE FILES ======================================================*/ #include /* stdout, putc(), printf(), getc() */ /*== MACRO DEFINITIONS (OBJECT-LIKE) ====================================*/ #define FALSE (0) #define TRUE (!FALSE) #define BLANKLINE printf("\n") #define MARGIN (2) /* Left Margin in PrintHeader() */ #define INDENT_SIZE (2) /* Indent relative to */ #define LMARG printc(' ', MARGIN) /* Print Left Margin */ #define INDENT printc(' ', INDENT_SIZE) /* Indent relative to margin */ #define EMPTYLOOP {} #define SIZE (11) /*== EXIT CODE DEFINITIONS ==============================================*/ #define EXIT_PASS (0) #define EXIT_FAIL (1) /*== MACRO DEFINITIONS (FUNCTION-LIKE) ==================================*/ #define PutC(c) (putc((char)(c),stdout)) #define PutD(d) (PutC( (char) ((d)<10)?('0'+(d)):('A'+(d)-10) )) #define GetC() (getc(stdin)) /*=======================================================================*/ /* SUPPORT FUNCTIONS ( functions not called directly by main() ) */ /*=======================================================================*/ int printc(char c, int n) { while ( (n--) && (c == putc(c, stdout)) ); /* EMPTY LOOP */ return n; } int PutV_u(unsigned int n) { unsigned int m; int i, count; /* Set m to the largest power or ten <= n */ for (m = 1; n/m >=10; m*=10) /* EMPTYLOOP */; /* Print out the digits in n one-by-one */ for (count = 0; m > 0; m /= 10) { for(i = 0; n >= m; i++) n -= m; PutD(i); count++; } return count; } int PutV_i(int n) { if (n < 0) { PutC('-'); return 1 + PutV_u(-n); } return PutV_u(n); } int PutS(const char *s) { int i; for(i = 0; '\0' != s[i]; i++) PutC(s[i]); return i; } int kgets(char *s, size_t size) { int i, c; /* Store characters until one of the three requirements fails */ for (i = 0; (i < (size-1)) && (EOF != (c = GetC())) && ('\n' != c); i++) s[i] = c; /* NUL terminate string */ s[i] = '\0'; if ( (EOF == c) || ('\n' == c) ) /* Entire string read and stored */ return 0; /* Count and discard remaining characters in buffer */ for (i = 0; (EOF != (c = GetC())) && ('\n' != c); i++) EMPTYLOOP; return i; } /*=======================================================================*/ /* PRIMARY FUNCTIONS ( functions called directly by main() ) */ /*=======================================================================*/ /*== FUNCTION PROTOTYPES (to Document Support Functions) ================*/ int printc(char c, int n); int PutV_i(int n); int PutS(const char *); /*== PRIMARY FUNCTIONS ==================================================*/ void PrintHeader(void) { LMARG; printc('=', (78 - MARGIN)); putc('\n', stdout); LMARG; printf("Course....... %s-%i (%s %i)\n",COURSE,SECTION,TERM,YEAR); LMARG; printf("Programmer... %s (%s)\n", PROGRAMMER, PROG_CODE); LMARG; printf("Assignment... %s (Rev %i)", ASSIGNMENT, REVISION); LMARG; printf("(Source Code in %s)\n", FILENAME); LMARG; printf("Description.. %s\n", TITLE); LMARG; printf(" %s\n", SUBTITLE); LMARG; printc('=', (78 - MARGIN)); putc('\n', stdout); } void Test_kgets(int length) { char String[SIZE]; int discarded; BLANKLINE; PutS("Enter a string that has exactly "); PutV_i(length); PutS(" characters (excluding the newline).\n"); PutS("STRING: "); discarded = kgets(String, SIZE); PutS("The kgets() function was passed a size of "); PutV_i(SIZE); PutS(".\n"); PutS("Characters stored: ["); PutS(String); PutS("] discarded: "); PutV_i(discarded); PutS("\n"); } /*=======================================================================*/ /* MAIN FUNCTION */ /*=======================================================================*/ /*== FUNCTION PROTOTYPES (to Document Primary Functions) ================*/ void PrintHeader(void); void Test_kgets(int length); int main(void) { PrintHeader(); BLANKLINE; Test_kgets(0); Test_kgets(SIZE/2); Test_kgets(SIZE-1); Test_kgets(SIZE-0); Test_kgets(SIZE+1); Test_kgets(SIZE*2); return EXIT_PASS; } /*=======================================================================*/ /* END OF SOURCE CODE FILE */ /*=======================================================================*/