CMSC 216 Exercise2 Text Manipulation Solved

45.00 $

Category:

Description

5/5 - (2 votes)

Message Body:
For this exercise you will implement two functions that manipulate text. The prototypes for the functions can
be found in file text manipulation.h.
1. int remove spaces(const char *source, char *result, int *num spaces removed);
This function places a copy of the source string in the out parameter result where all leading and trailing
spaces have been removed. If the out parameter num spaces removed is different than NULL, the function will set the integer associated with the parameter to the number of spaces removed. The function
will return one of two values: FAILURE or SUCCESS (see file text manipulation.h).
a. FAILURE – if the source string is NULL or its length is 0. In this case the result string is not assigned
a new value (it keeps its original value).
b. SUCCESS – if spaces can be removed or no spaces are present.
2. int center(const char *source, int width, char *result);
This function generates a new string into the result out parameter where the source input string has
been centered in a string with length specified by the width parameter. Center the string by adding (to
the left and right of the original string) a number of spaces that corresponds to (width – source string
length) / 2. Notice that the resulting centered string has a length that is less than width when (width –
source string length) is odd. For example, if we were to center “dogs” in a field with of 7, the resulting
string is “ dogs ” (1 space to the left, 1 space to the right). The function returns one of two values:
SUCCESS or FAILURE (see file text manipulation.h).
a. FAILURE – if source is NULL, if source length is 0, or if the width is less than the source length.
b. SUCCESS – if item is centered.
You should look at the public tests in order to understand the functionality associated with the functions you
must implement. For this exercise, you can assume the user will not provide a string containing only blank
characters. In addition, if a string has multiple words, the spaces between those words must be preserved.
4 Requirements
1. Unlike other assignments for this course, you can work together with other classmates, but you may
NOT exchange any code.
2. If you are having problems with your code, use the class debugging guide available at:
1
3. Your grade is based on the results obtained from the submit server. It is your responsibility to verify that
your program generates the expected results on the submit server.
4. Your code must be written in the file text manipulation.c.
5. Do not add a main function to the text manipulation.c file.
6. To compile a public test, compile your text manipulation.c file along with a public test file. For example:
gcc public01.c text manipulation.c
7. One source of bugs is forgetting to add a null character to a string.
8. All your C programs in this course should be written using the compiler gcc with the options defined at
9. Your program should be written using good programming style as defined at
10. You just need to implement the two functions described above. You may write additional functions if
you want, but define them as static.
11. Do not change the text manipulation.h file provided.
12. You are encourage to define your own tests (similar to public01.c, public02.c, etc.).
13. You can use the C string library (string.h).
14. Do not use any functions from ctype.h.
15. You can assume the result char array will be long enough to contain the string functions will create.
16. Do not use dynamic memory allocation.
17. The example ptr add sub overview.c reviews concepts associated with this exercise.

  • text_manipulation-knfmlb.zip