Lab assignment: C/C++ char arrays.

Preliminary Information:

To work with C++ in the lab you will need to do the following steps:

1) Write the program. This can be done with a text editor, for instance the same editor you use for the quizes. For this lab the skeleton of the program will be provided to you.
2) Save the file containing the program as a .cpp file. You can save it on your desktop or in your home folder.
3) Compile the program: Start the terminal. Go to the folder where the program was saved by using cd. Suppose the file is called lab11.cpp. You can compile it by typing: g++ lab11.cpp -o lab11.exe and pressing ENTER. This command translates the program into a binary executable file named lab11.exe in the same folder.
4) Execute the program: After step 3, if you do not get any error messages, a file named lab11.exe should have been created in the same folder. To execute the file type ./lab11.exe and press ENTER.

LAB 11. Char arrays

The lab today deals with strings in C. You need to write two methods to implement the two functions strcmp and strstr. A description of the two functions is provided in the file string.h. The file can be consulted in this link.

The function strcmp receives two character pointers as paramters in input and returns -1 if the word represented by the first array precedes the word represented by the second array, 0 if the two words are equal, and +1 if the first word follows the second word. The function is used usually to compare arrays of characters representing strings. The name of the array is the pointer to the first character, so when the array is passed as a parameter to strcmp by using its name in the invocation, what the strcmp is really receiving as parameter is the pointer to the first char element of the array.

The function strstr receieves two character pointers as well as parameters in input and returns a pointer to the first occurrence of the second within the first, or NULL if none is found. In other words it can tell if the second word (or sentence) is contained in the first word (or sentence) and if yes, it returns a pointer to the first.

The .cpp file below contains code to get you started:

lab11.cpp

The file contains the main. Some functions are called inside the main. You MUST CREATE AND WRITE THE FUNCTIONS.

Stage 1 & 2:

Create a function named compareStrings that implements the method strcmp. The main contains calls to both methods and prints out the results for comparison. You MUST CREATE AND WRITE THE CODE of the method compareStrings. You **MUST NOT REUSE** the method strcmp inside the method compareStrings. The best way to do this inside is to scan both arrays in parallel and compare the elements one by one. The execution of stage 1 might look like below:

Notice how the strcmp and compareStrings methods return the same values.

Stage 3:
Implement the method strstr. The main contains already a call to a method named my_strstr, that you must create and write the code for. **YOU MUST NOT REUSE** the method strstr inside your method. The execution of the code might look like below:

The method strstr receives both strings in input and looks inside the first string in order to find the second string. If it finds it,
it returns a pointer to the first character inside the first string, that is to the starting point of the second inside the first string. In the first example above the method strstr returns a pointer to the letter C inside the array that contains the word "ILikeCPlusPlus". Your method should behave in the same way. In the main both pointers returned by the two methods are used to print out the portion of the word.

The turnin command for this assignment is turnin -c cs102 -p lab11 lab11.cpp


If you write code on other files (not necessary) turnin the other files as well.

Solution:

lab11.cpp