Homework 13: Trim Spaces
Due: Tuesday 12/13 by 1pm
even more practice with strings.
Your assignment is to write a program function that trims (i.e., removes) leading spaces, extra spaces and trailing spaces from a given string. A sample run of your program, might look like:
PT% ./a.out Enter a string: Space, the final frontier, these are the voyages Trimmed string = "Space, the final frontier, these are the voyages" PT%
The function prototype of the function you have to implement is:
void trim_str(char *new_str, char *old_str) ;
The string that your function has to trim is old_str. You should not alter the characters in old_str. Instead the characters from old_str minus the extra spaces are copied to new_str. You should assume that new_str has enough space to hold the converted string.
Here's a starting point of your program: trim.c. It includes a suggested algorithm for trimming the string in the comments.
NotesSome implementation notes:
- All leading spaces should be removed.
- If old_str has only one one trailing space, then new_str should have just one trailing space.
- If you encounter a segmentation fault, most likely your program overshoots the end of the array somewhere.
SubmittingUse the script command to record yourself compiling and running your program. Run your program several times with different types of input (e.g., with extra spaces at different places). Submit your C source code and the typescript file as usual:
submit cs104_chang hw13 trim.c submit cs104_chang hw13 typescript