ECE-1021

HOMEWORK #6

(Last Mod: 27 November 2010 21:38:38 )

ECE-1021 Home


Program A: String Functions

Using only the functions getc() and putc()  (both are in stdio.h), write equivalent versions of the following functions, prefacing each with "my_". A couple of examples are provided below.

Do not worry about error trapping or returning the same results under error conditions as the standard functions. This is not to imply that doing so is not important, but that the first step is to get the functions behaving properly when they are used properly. Then you can go back, if there is time, and work on making them more robust and/or a more complete match to the standard functions. 

From stdio.h"

From ctype.h: (only deal with the default locale)

From string.h:

The majority of these functions require only a couple of lines of code. Also, many of them are special cases of others or can otherwise make use of other previously defined functions. Don't reinvent too many wheels.

One function that you will need to use in some of the others is the strdup() function which creates a new string that is a duplicate of the string that is passed to it. Since this involves dynamic memory allocation, the code for that is given below.

char *my_strdup(const char *s)

{

// 1) TASK: Determine the length, n, of the present string.

// 2) TASK: Allocate sufficient new memory for a string of size n.

// 3) TASK: Copy the present string to the new memory.

// 4) TASK: Return a pointer to the new string.

 

int n;

char *newstr;

 

n = my_strlen(s);

newstr = (char *) malloc(n+1);

my_strcpy(newstr, s);

return(newstr);

}

Notice that this function has to perform tasks that can be performed by other functions and so, instead of duplicating the effort, those functions are simply invoked as needed.

To use the malloc() function, you must include one of a number of header files that give access to it. The simplest one is usually stdlib.h.

If you use the my_strdup() function, then you are responsible for freeing up the memory that was allocated by calling the free() command and passing it a pointer to the memory that is to be freed. As an example of how this can be done, the my_stricmp() function is implemented below:

int my_stricmp(const char *s1, const char *s2)

{

// 1) TASK: Make temporary copies of the two strings.

// 2) TASK: Convert both temporary strings to the same case.

// 3) TASK: Compare the converted strings and store result.

// 4) TASK: Free the memory used for the temporary strings.

// 5) TASK: Return the stored result.

 

int result;

char *new_s1, *new_s2;

 

new_s1 = my_strdup(s1);

new_s2 = my_strdup(s2);

 

my_strupr(new_s1);

my_strupr(new_s2);

 

result = my_strcmp(new_s1, new_s2);

 

free(new_s1);

free(new_s2);

 

return(result);

}

The program that you submit should be a demonstration driver that shows each of your functions being used at least once. Try to exercise your functions sufficiently so that the driver demonstrates that they actually work - although you do not need to be exhaustive in your testing. For instance, for the case sensitive string comparision function you might use two examples using two strings that consist of the same characters except for case differences. In one example you give it the strings in the opposite order. You might then give it the same string for both to show that it handles things properly. Next you might then pass those same strings to the case insensitive version of the comparison function and show that it says they are equal in all three cases.

For the ctype.h functions that return logical results, don't worry about returning the exact values that the standard functions return. What is important is that you return a zero if the result is false and something other than zero if the result is true.