Question
Write a C function that inputs a pointer to a string and a pointer to a buffer. The function then scans through the string removing
Write a C function that inputs a pointer to a string and a pointer to a buffer. The function then scans through the string removing leading and trailing whitespace and replacing any run of whitespace within the string with a single space. Your function should follow this prototype:
void tighten(char *oldstring, char* newstring, int length);
The first argument is a pointer to a null-terminated string sitting somewhere in memory. The second argument is a pointer to the first char of a buffer of size length. The function will copy a modified version of the old string into the buffer. For example, if the original string is
Be true to the dreams of thy youth!! the new string in the buffer would be Be true to the dreams of thy youth!!
Of course, the new string will be terminated with a null. Don't make any assumptions about what is in the buffer to start with. It may have been used several times before. Stop copying characters from the old string to the new string when the new string is full (has length-1 characters). Be sure to terminate the new string with null. The function should not print anything out nor use global variables. Study the example programs strcpy() and strlen() first before you write this. This function is only 20 lines or so if done sensibly.
Be sure your function deals correctly with empty strings, strings of one character, strings of all white space, and very long strings. It should make no assumptions about the old string length. A string of all white space should be output as an empty string. An empty string should be output as an empty string. An empty string is one which has null in the first byte. Use the library function isspace for detecting white space.
Test your function by writing a main() program that reads standard input line by line and outputs a tightened version of each line. Use gets() to input each line, then process the line with tighten(), then use puts() to output each tightened line. Maintain the line structure of the input file. Assume that input lines are under one kilobyte long.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started