This assignment is Due on: Day: Wednesday, April 15, 1998 Time: Before 6:00 PM WARNINGS: TURN IN THIS ASSIGNMENT ELECTRONICALLY USING "turnin". LATE ASSIGNMENTS WILL NOT BE ACCEPTED. ---------------------------------------------------------------------- The purpose of this homework is to reinforce the use of dynamic allocation with malloc(3C) realloc(3C) free(3C) Use of arrays and pointer arithmetic will also be reinforced. Dynamic allocation is powerful but dangerous. Be sure to read Weiss pp 163-173 and Weiss pp 207-208. You should also the material in Chapter 8 on strings - in particular, pp 180-192 and 194-202 contain some information that will be useful in completing this homework. In addition to this .nfo file, you will need a file called "palin.in", which is a text file with some test data in it. This file can be retrieved by doing a "getjob hw1". -----------------------Start Answer Here----------------------- [p0] 2.0 points What does malloc(3c) do? What is its return value? ------------------------End Answer Here------------------------ One function that is in most string libraries but is not in the ANSI C standard is strdup(3c). Here is a description of strdup(3c), from the oh-so-useful man page ("man strdup"): char *strdup(const char *s1); The strdup() function returns a pointer to a new string which is a duplicate of the string s. Memory for the new string is obtained with malloc(3), and can be freed with free(3). The strdup() function returns a pointer to the duplicated string, or NULL if insufficient memory was available. Write code that performs the functionality of strdup using malloc(3C) and ANSI string functions (a list of string functions can be found by doing "man string"). You should not "rewrite" any of the ANSI string functions - they are there for you to call in writing your own strdup(3c). -----------------------Start Answer Here----------------------- [p1] 6.0 points Include the COMPLETE source code for your implementation of strdup(3c) here. ------------------------End Answer Here------------------------ The following function is supposed to print the argument list that is passed to it, in reverse order. /* print argument list in reverse order */ void revarg( int argc, char *argv[ ] ) { char *str; while( argc > 0 ) { str = strdup( argv[ --argc ] ); printf( "%s\n",str ); } } However, there are two serious problems with this implementation. -----------------------Start Answer Here----------------------- [p2] 6.0 points What are the two problems? How can they be fixed? ------------------------End Answer Here------------------------ The next few problems entail writing a program to recognize if a word or phrase is a palindrome (i.e. is the same if reversed). Write a non-recursive function with the following prototype: char *palin( const char *str ) that takes a null-terminated string as its parameter and returns NULL if the string is indeed a palindrome or a pointer to the leftmost character which violates the palindrome property. As an example of a string that "violates the property", consider "ABCDEBA". If a pointer to that string were passed into palin(), then palin() would return a pointer to the 'C' character. Only alphabetic characters are to be considered, and case does not matter, so "Pull up if I pull up!" is a palindrome. In other words, any non-alphabetic character is to be strictly ignored when checking for a palindrome. -----------------------Start Answer Here----------------------- [p3] 10.0 points Include the COMPLETE source code for your implementation of palin() here. ------------------------End Answer Here------------------------ Since there can be an arbitrarily large palindrome on multiple lines, we need a good way to read it in. Functions such as gets(3s) are limited, as they only read in a predetermined amount, which causes problems when larger than expected input is given, resulting in buffer overruns, dumping core, or possible exploits. The method you will use is to read the string in character by character with getchar(3s) and dynamically allocate space for the string as you read it in. Each time you read in a character, you will have to realloc one more byte, being sure to check for possible errors and to avoid memory leaks. Write a function with the following prototype: char *getstr( void ) This function will read a series of one or more lines of text (one character at a time, using getchar(3s) ), and concatenate all the lines into a single string. If a newline character is encountered within the input string that is being read, then it is replaced in the returned string with a space character. Successive calls to getstr() return the "next" string from input, until an error or EOF is detected - if an error occurs, then getstr() will free(3c) the input string and return NULL, otherwise it returns a pointer to the string that was read in. Successive palindromes are delimited from one another by one or more empty lines (i.e. lines on which no character data appears). In this case, the newline characters are NOT replaced by spaces - you should stop reading the current input string when an empty line is encountered, or when getchar(3s) returns EOF. Empty lines at the beginning of each "next" input string are to be ignored. Note that you will need to null-terminate the string yourself after it has been completely read in. -----------------------Start Answer Here----------------------- [p4] 14.0 points Include the COMPLETE source code for your implementation of getstr() here. ------------------------End Answer Here------------------------ You are almost done now. All that is left to write is main(). A file may have many strings (each spanning multiple lines) that will be checked to see if they are palindromes. Your main() function will need to get each string in turn and check it. This involves a loop that calls getstr() and palin(), until all input strings have been readi and checked. If a string is indeed a palindrome, print "Palindrome!" Otherwise, print the string up to and including the leftmost character which violates the palindrome property. -----------------------Start Answer Here----------------------- [p5] 6.0 points Include the COMPLETE source code for your implementation of main() here. ------------------------End Answer Here------------------------ An important part of programming is testing. It is an important skill to be able to find bugs before the end-user does, so programmers should be skilled at designing encompassing tests. Write down 3-5 cases to test palin(). For each test case, explain why it is a good case to test against the specifications. Also say whether it passes or fails. The more specifications your tests cover, the more points you get. -----------------------Start Answer Here----------------------- [p6] 6.0 points Include the COMPLETE text of your test cases here. ------------------------End Answer Here------------------------