Details of this Paper

PA202 - Assignment - 2 Tasks




Question;Task #1 ? Implementing and testing word_reverseThe first function to implement is meant to reverse a single word inside a string.Some reading before you startBefore to start working on implementing anything, read the entire source and make sureyou understand it so that you are able to add to it without breaking everything.In addition, spend some time reading the requirements in this document and add sometest functions in tests.c to reflect the tests you think your program ought to pass to meetthese requirements.Implementing your solutionThe word_reverse function operates on a string which is assumed to hold a singleword. It will take all its characters, except the end-of-string marker ?\0?, and reverse theirorder. You will reverse the order in the string itself, by swapping characters two by two.Testing your solutionYou will then write tests to validate your solution. For task #1, we are ready to test onlythe reversal of single words strings. The ?mock implementation? provided to you forwords_initialize will make it so that the program assumes your string has a single word.Please note that the functions words_modify and words_reverse both modify the stringsthat are passed to them. If you use them and specify as parameter a literal strings, thingswill not work. Instead, you should always call them with a dynamically allocated copy ofa string as in the example below,char* stringParameter = strdup("hello"),words_modify(stringParameter),strdup is a tool we are going to use more and more in our assignments. This functiontakes a string and returns a copy of it. To do so it uses dynamical memory allocationwhich we will study in module [203].Similarly, you will have to use strcmp to test if two strings are the same. There is noway to do so using the == operator as you would to compare two integers.char* stringParameter = strdup("hello"),words_modify(stringParameter),int result = strcmp(stringParameter, "olleh"),TEST("reversing [hello]", result == 0),Task #2 ? Implementing and Testing words_initializeThe second function you will have to implement will be the one actually locating thebeginning of each word in the string provided as parameter str to it. In order to help youdetermine which characters are ?separators? between words, a function is_separator isprovided to you. Do not worry about its implementation, we?ll detail it in module [203].Implementing your solutionThe logic to identify words is to- Start at the beginning of your string and skip any character which his recognized byis_separator as a separator.- The first non-separator character you meet is the beginning of your first word.- You now keep skipping all non-separator characters which follow until we meeteither the end of string marker ?\0? or a separator.- When we do, we now know where the first word ends and we replace this characterby ?\0? to end the substring for this word.- If we didn?t meet the end of string marker yet, we now skip all following separatorsuntil? you guessed it, the beginning of the next wordThe address of the first character of each word will be stored in an array of pointerswhich is also passed as parameter to your function. This array has a maximal size, also aparameter, which means that your function should not drop identifying any further wordsin the string if it already reached the maximal number of words it is able to store in thepointer array.So, in summary, you iterate over the string, find the first character of each word andstore its address at the right index in your array parameter named words. Then youchange the character immediately following the last character of each word so that itbecomes a ?\0?.Together, these two actions will result in you having the address of the first character ofevery word in your pointers array. When we will use this address, we will be able todisplay or process each word as if it was its own string. This is due to the fact that weterminated each segment of the original string containing a word with a ?\0?.Later, after we are doing performing some processing on each of these substrings, wewill be removing these ?\0? and replacing them by spaces. This is not for you to do, thefunction words_modify will do that for you by invoking word_handle_marker afteryou are done with your parts.


Paper#36354 | Written in 18-Jul-2015

Price : $57