CMSC216 Excercise 2-Text Manipulation Solved

35.00 $

Description

5/5 - (5 votes)

1 Objectives

To practice strings and pointers.

2 Overview

First copy the directory text manipulation in the grace cluster under the exercises directory. It contains the .submit file that allows you to submit.

3 Specifications

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 func- tion 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).
    1. 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).
    2. 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).
    1. FAILURE – if source is NULL, if source length is 0, or if the width is less than the source length.
    2. 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:
         http://www.cs.umd.edu/~nelson/classes/resources/cdebugging/
    

1

  1. 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.
  2. Your code must be written in the file text manipulation.c.
  3. Do not add a main function to the text manipulation.c file.
  4. 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
  5. One source of bugs is forgetting to add a null character to a string.
  6. All your C programs in this course should be written using the compiler gcc with the options defined at
       http://www.cs.umd.edu/~nelson/classes/resources/setting_gcc_alias/
    
  7. Your program should be written using good programming style as defined at
       http://www.cs.umd.edu/~nelson/classes/resources/cstyleguide/
    
  8. You just need to implement the two functions described above. You may write additional functions if you want, but define them as static.
  9. Do not change the text manipulation.h file provided.
  10. You are encourage to define your own tests (similar to public01.c, public02.c, etc.).
  11. You can use the C string library (string.h).
  12. Do not use any functions from ctype.h.
  13. You can assume the result char array will be long enough to contain the string functions will create.
  14. Do not use dynamic memory allocation.
  15. The example ptr add sub overview.c reviews concepts associated with this exercise.
  • Exercise2-jvkzt4.zip