CSC 101 Lab Week 7

String Functions

DUE: Tuesday, 17 May 2011, at the end of lab

Implement the functionality of the following string functions:

  1. strlen
  2. strcmp
  3. strncpy
You should have three separate functions that mimic precisely the functionality of the equivalent functions in the string.h library. You may use the string.h functions to test and become familiar with their output and behavior, but your program should not use the library functions, or include the string.h library.

Your program should include a driver routine that tests each of the above functions. Your compiled program should take three command line arguments: two strings and one integer. The two strings should be passed to each of your functions, and the integer should be used as the number of characters to copy in the strncpy function.

Style

Your program will be evaluated for style, especially correct indentation, comments, and variable usage. You should have no global variables in this assignment. Be sure to use indent -kr -nut on your program.

Sample Output

$ stringtest Hello Lab 2
Output of strlen: 5 (Hello)     3 (Lab)
Output of strcmp: -1    Hello is less than Lab
Copying the first 2 characters of Hello to Lab yields: Heb
$ stringtest John Jacob 4
Output of strlen: 4 (John)     5 (Jacob)
Output of strcmp: 1    John is greater than Jacob
Copying the first 4 characters of John to Jacob yields: Johnb
$ stringtest yummy yummy 2
Output of strlen: 5 (yummy)     5 (yummy)
Output of strcmp: 0    yummy is the same as yummy
Copying the first 2 characters of yummy to yummy yields: yummy

Testing

You should write checkit_* tests for each of your functions. Duplicate your tests using the string.h library functions to demonstrate that your program does the same thing as the library functions.

Makefile

Name your program stringtest.c and create a Makefile for this program that contains the following compile command:

gcc -ansi -pedantic -Wall -Werror -o stringtest stringtest.c

Submitting Your Program

After your program has been checked in person during lab, submit it on vogon using the command:
    handin djanzen 101_lab7 stringtest.c Makefile
Complete this survey.