Question: complete the readLine function below, the function reads line from stdin. more instructions below in the comments #include #include /* * Function: ReadLine * Usage:
complete the readLine function below, the function reads line from stdin. more instructions below in the comments
#include
/* * Function: ReadLine * Usage: s = ReadLine(); * --------------------- * ReadLine reads a line of text from standard input and returns * the line as a string. The newline ' ' character that terminates * the input is not stored as part of the string. */ char *ReadLine(void);
int main(int argc, char *arvg[]) {
char *name;
printf("Enter a name to test your ReadLine function : "); name = ReadLine(); printf("User entered : %s ", name); free(name);
return 0; }
/* * IMPLEMENTATION of ReadLine(); * Function: ReadLine * Usage: s = ReadLine(); * --------------------- * ReadLine reads a line of text from standard input and returns * the line as a string. The newline ' ' character that terminates * the input is not stored as part of the string. * * In contrast to standard I/O functions (e.g., scanf with "%s", fgets) * that can read strings into a given static size buffer, ReadLine function * should read the given input line of characters terminated by a newline * character (' ') into a dynamically allocated and resized buffer based on * the length of the given input line. * * When implementing this function you can use standard I/O functions. * We just want you to make sure you allocate enough space for the entered data. * So don't simply allocate 100 or 1000 bytes every time. * If the given input has 5 characters, you need to allocate space for 6 characters. * * Hint: initially dynamically allocate an array of char with size 10. * Then, read data into that array character by charecter (e.g, you can use getchar()). * If you see ' ' char before reading 10th character, you are done. And you know the * exact length of the input string. So, accordingly allocate enough space and copy the * data into new char array, insert '\0' instead of ' ' and free the original array. * Then return the new string. However, if you DO NOT see ' ' char after 10th character, * then you need larger space. Accordingly, resize your original array and double its size * and continue reading data character by character as in the first step... * Hope you got the idea! * * Also please check for possible errors (e.g., no memory etc.) and appropriately handle * them. For example, if malloc returns NULL, free partially allocated space and return * NULL from this function. The program calling this function may take other actions, * e.g., stop the program! */
/* IMPLEMENT SOLUTION HERE BASED ON THE COMMENTS ABOVE */ char *ReadLine() { }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
