From the course: Complete Guide to C Programming Foundations

Chapter challenge: Processing input - C Tutorial

From the course: Complete Guide to C Programming Foundations

Chapter challenge: Processing input

(bright music) - [Instructor] Here's the code skeleton for this chapter's challenge, where your task is to write a function that processes input. The main function calls the get input function twice. Its argument is the number of characters to process, which includes the null character. The return value is a string, the text input. The function does two useful things, it allocates storage for input based on the size desired, and it ensures that the string input doesn't terminate with a new line character. Your task is to write the get_input function. It must allocate storage based on the value passed. If storage cannot be allocated, the program quits. Once storage is allocated the function reads standard input. The input is stored in the freshly allocated buffer. Ensure that text input is scanned, and if the new line is found, it's removed, replaced with a null character. The function returns the modified string. For hints, remember that this chapter is all about pointers. Yes, you must allocate storage in the get_input function, but I also want you to use pointer notation as you manipulate the text input. Remember to free the allocated memory when the program quits. This challenge should take you about 10 minutes to complete.

Contents