CS12, Lab 6

Week of May 13 – 17

 

 

Topics 

 

·    Recursion

·    Character strings (C-style)

·    Practice with pointers

·    Practice with Unix tools

 

Please do all the work for this lab in the Linux environment.  This includes editing the source code, compiling and running the program.  Extra credit (1 point) if you also write a makefile.

 

To do

 

Write a program that will prompt the user for two strings and check if they are equal.  Read the strings into character arrays (c-style strings), not string objects.  Declare two pointes to point to the arrays and then allocate space for both strings.  Assume the strings are no longer than 15 characters (don’t forget that you need an extra space for the null character at the end of the string.)  After space is allocated, you can use cin >> to read the strings (this will also take care of placing a ‘\0’ at the end of the string).

 

There are many ways to check if two strings are equal.  In this lab, you will do this with a recursive function.  The function will take as parameters two pointers to char that point to the two strings.  The function does the following:

 

·    If the pointers point to different characters, return false

·    If both pointers point to the null character, return true

·    If both pointers point to the same (non-null) character, advance both pointers and call the recursive function again, returning the result of the recursive call.

 

Test your function with different input strings to make sure it works correctly with equal and with unequal strings.