# CS-211 Fall 2015 Lab 7 Arrays and Pointers

### Background

We learned in lecture on Monday that arrays and pointers are really the same thing. Today we get to prove it. Today we are going to write two implementations of the string comparison function, strcmp, one using array notation, and another using pointer notation.

One more background item to cover... how we compare two strings. For this lab, we will use the same conventions used in the standard libray function, strcmp to compare two strings. If you compare the character vector in a to the character vector in b, you should return -1 if a is less than b, zero if a equals b, and +1 if a is greater than b. The only case in which a equals b is if a and b are the same length, and every character in a exactly matches the corresponding character in b. If two strings are not equal, then they may share an equal prefix string, but after that prefix, there is at least one character that is different between a and b. We determine whether a is less than b or a is greater than b based on the first differing character. If that first differingg character in a is less than the corresponding character in b, using the ASCII character codes, then the string in a is less than the string in b. If the first differing character is greater, then a is greater than b.

### Getting Started

Download the code that has been started for you in lab7.tar.gz. Use the command tar -xvzf lab7.tar.gz. This will unpack the tar file and make a new lab7 subdirectory. The lab6 directory will have a Makefile, and the C code to start from in file compStr.c

### The compStr.c File

The compStr.c file starts of with the standard includes for standard library functions, and prototypes for the three lower level functions in the file.

What follows is just the normal function definitions that we are used to seeing. I've included a description of each of these functions below.

• main(int argc,char **argv) The main function performs the following actions:
• Checks to see if the compStr command was invoked with at least two arguments. If not, issues an error message and quits with a "1" return code to indicate failure.
• Invokes the strcmp_a function on the first two arguments, and saves the result in ra.
• Invokes the strcmp_p function on the first two arguments, and saves the result in rb.
• Invokes the standard library strcmp function on the first two arguments, and saves the result in rc.
• Prints the results of comparing the first two argument strings when compared with the strcmp_a function.
• Prints the results of comparing the first two argument strings when compared with the strcmp_p function.
• Prints the results of comparing the first two argument strings when compared with the standard library strcmp function.
• If all three comparison are the same, returns a success (0) return code. Otherwise, returns a failure (1) return code.
You don't have to change the main function for this lab.
• strcmp_a(char a[],char b[]) The strcmp_a function is a function you need to implement. You need to determine if a is less than, equal to, or greater than b, using the definitions described above. You may use array notation to implement this function, but you may not use the standard library functions, and you may not use pointer notation. The point of this part of the lab is to figure out how to implement the string compare function using array notation.
• strcmp_p(char *a,char *b) The strcmp_p function is a function you need to implement. You need to determine if a is less than, equal to, or greater than b, using the definitions described above. You may use pointer notation to implement this function, but you may not use the standard library functions, and you may not use array notation. The point of this part of the lab is to figure out how to implement the string compare function using pointer notation.
• compString(int r) This is a utility function that simply returns a string to make printing pretty based on the result of a string comparison. You don't have to change the compString function for this lab.

### Using The Makefile

The Makefile file in the lab7 directory contains "targets" which allow you to do several things using the make command. The following targets are supported:

• make compStr - Makes the compStr executable if you have changed the source code.
• make test - Makes the compStr executable if you have changed the source code, and then runs that executable using the testastr and testbstr as parameters.
• make clean - Removes the compStr executable.

### Lab Report

Download and edit the following file: lab7_report.txt. Then submit your editted file on Blackboard in the Lab_07 report area.