/*---------------------------------------------------------- Elizabeth Sklar MC140.0X 6 December 2000 This program contains a function that reads one line of input (that may contain whitespace) from the keyboard and returns a string (that contains the whitespace). ----------------------------------------------------------*/ #include /*--------------------------------------------------------*/ /* readInput */ /* */ /* This function reads characters from the keyboard and */ /* stops when a newline ('\n') character is read. */ /* */ /* The function places all the characters -- which may */ /* include spaces -- into a string (except the '\n') and */ /* returns that string. */ /* */ /* The function allocates storage for the string it */ /* returns, so a calling function does not need to */ /* allocate a string and only need allocate a pointer, */ /* then assign that pointer to the value returned by the */ /* function. */ /* */ /*--------------------------------------------------------*/ void readLine( char buf[],int bufsize ) { unsigned int k; char c = '\0'; int i = 0; while (( i < bufsize ) && (( k = fgetc( stdin )) != EOF ) && (( c = (char)k) != '\n' )) { buf[i++] = c; } buf[i] = '\0'; } /* end of readLine() */ int main() { char buf[1024]; printf( "enter a string: " ); readLine( buf,1024 ); printf( "string=%s\n",buf ); }