ECE-1021

HOMEWORK #5

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

ECE-1021 Home


Objectives


String Input

Write the following function to get string input from the keyboard.

int kgets(char *s, size_t size);

This function accepts a pointer to memory that has been allocated a minimum of size bytes. The size_t data type is simply an integer data type suitable for suitable for representing the sizes of objects. It is a defined by the compiler developer using a typedef statement which is very similar to a #define statement. On most compilers, it is an alias for an unsigned int. Using size_t instead of int or unsigned int just makes your code more portable to other compilers.

The function reads characters from the standard input device using getc() until either a newline character is read, an EOF value is returned by getc(), or (size-1) characters have been read. The string is then null terminated. Neither the newline character nor EOF (which is not an ASCII code to begin with) is stored in the string.

If the function did not encounter a newline character or EOF before reading in (size-1) characters, the function should continue reading in (and discarding) characters from stdin until a newline character or EOF is read.

The function should return the number of characters that were discarded.

Your test program should include strings that are empty, strings that are well short of the size passed to the function, strings that are one less, equal to, and one more than the size passed to the function, and strings that are well in excess of the size passed to the function.

For each string read in, your program should print out the size that was passed to the kgets() function, the string that was stored, and the number of characters that were discarded.


String Length

Write the following function that returns the length of a string.

int strlen(const char *s);

The length of the string is defined as the number of characters in the string not including the null-terminator.

String Copy

Write the following function that copies one string into another.

char *strcpy(char *dest, const char *src);

The return value is a pointer to the destination string.

String Concatenation

Write the following function that appends one string to another.

char *strcat(char *dest, const char *src);

The return value is a pointer to the destination string.

String Conversion to Uppercase

Write the following function that converts all characters within a string to upper case.

char *strupr(char *s);

The return value is a pointer to the destination string.

String Conversion to Lowercase

Write the following function that converts all characters within a string to upper case.

char *strlwr(char *s);

The return value is a pointer to the destination string.


The following two functions are Extra Credit

Dynamic String Copy

Write the following function that allocates new memory for a copy of a string.

char *dstrcpy(const char *src);

The return value is a pointer to the newly allocated destination string.

Dynamic String Combination

Write the following function that allocates new memory for the combination of two strings.

char *dstrplus(const char *s1, const char *s2);

The return value is a pointer to the newly allocated destination string.


Test Program

Your test program should use each of these functions in such a way that it is reasonably clear that they are performing properly. It does not take extensive code to do this.

Do not forget to perform garbage collection on the dynamically allocated memory.