CS 2213/2211
 Advanced Programming
 Spring 2005

 Recitation 6:  Strings and Pointers
    Week 6: Feb 22-24
 Due (on time): 2005-03-01  23:59:59
 Due (late):        2005-03-06  23:59:59

Recitation 6 should be submitted following directions at: submissions with deadlines
  • 2005-03-01  23:59:59 (that's Tuesday, 1 March 2005, 11:59:59 pm) for full credit.
  • 2005-03-06  23:59:59 (that's Sunday, 6 March 2005, 11:59:59 pm) for 75% credit.


Introduction: This recitation works on three areas:

First refer to the page String Functions for important help with this recitation. This page presents several string functions to use in the recitation and for study.


A Reverse Function, Array Index Notation: Use array index notation to write a function with prototype void reverse(char s[]);. (This function should not have any stars in it.) This function should reverse the characters of its parameter string in place, that is, right in the string, without creating or using a copy. As a hint, start with two integer indexes: i and j, where initially i gives the index of the start of the string, that is, 0, and j gives the index of the last character in the string, just before the null character at the end. Then use the standard three lines to interchange these characters. Next increment i and decrement j and repeat. Keep going as long as i < j.


A Reverse Function, Pointer Notation: Write a function with prototype void reverse2(char *s); that will reverse its string parameter in place as above, but written in pointer notation -- no use of square brackets. You can use the string comparison functions above as a model for converting to pointer notation. As a hint, if s is a pointer pointing to the first character of a string, then t = s + strlen(s)-1 will point to the last character just before the null character.


A driver program to test both reverses above:
  1. Test strings of length 0, 1, 12, and 13 (or any two decent-sized even and odd sizes).


Palindromes again: The main project work:

  1. Start with the line-by-line file copy program that was the foundation for Recitation 3. Modify this program so that the letters on a line are inserted into a string, first converting them to lower-case. (Letters only and not the newline.) At the same time output the entire line.
  2. The string in step 2 into which you insert this the letters from the each line as your are reading can be just a fixed array of char that is used over and over, perhaps 100 characters long. (Such storage is called a buffer.) This string can be declared as a local array or allocated using malloc.
  3. Allocate storage for another string and make a copy of the first string in the new storage, using one of the string copy functions at string functions.
  4. Use one of the reverse functions that you wrote above to reverse the second string in place.
  5. Use one of the string comparison functions in string functions to compare these two strings. If they are equal, print that they ARE palindromes, and otherwise print that they are NOT.
  6. Use the C free function to deallocate the storage allocated in step 4 above.
  7. Handle the same set of test data as before, repeating steps 2-7 over and over for each input palindrome.


What you should submit: Refer to the submissions directions and to deadlines at the top of this page. The text file that you submit should first have Your Name, the Course Number, and the Recitation Number. The rest of the file should have the following in it, in the order below, and clearly labeled, including at the beginning the appropriate item letters: a, b, c, etc.

 Contents of email submission for Recitation 6:

Last Name, First Name; Course Number; Recitation Number.

a. A program for item 1 above, along with the output of a run. Note that you should be testing both reverse functions.

b. A probram for items 2-8 above, along with the output of a run. This run should include all the test data for Recitation 3: palindata.txt.


Revision date: 2005-02-25. (Please use ISO 8601, the International Standard.)
Copyright © 2011, Neal R. Wagner. Permission is granted to access, download, share, and distribute, as long as this notice remains.