CS115 Exercise: Strings in C and C++

Part I - Comparison of C Strings and C++ Strings

You should do part 1 on Hercules using CC. To log into Hercules from your linux computer use ssh:

ssh hercules
then follow the prompts. When you are done Part 1 try other compilers like g++ on Hercules and g++ on a Linux machine. What you see may surprise you.

  1. Compile the program using CC and the -g flag.

  2. Run the code and observe what happens.

  3. Use dbx with the name of your executable to get into the debugger.

  4. List some code.

  5. Set a break point at line 42.

  6. Run the code (inside the debugger).

  7. It should stop at line 42 (or 44). At this point print off all of c string variables: C_str1, C_str2, and C_str3, and all of the string variabes: C_Plus_str1, C_Plus_str2, and C_Plus_str3.

  8. Step past two more lines of code and try printing the variables again. What do you notice this time?

  9. Quit dbx. Do not type any more commands from this terminal so that you can show your lab instructor your debugger run.
  10. Answer the following questions:
    1. What's happening?
    2. Why?
    3. How would you fix it?

Part II - Manipulating Strings with C String Arrays and Functions

Write a simple program that asks the user for a Canadian Postal Code and then verifies that code by:

Remember to use #include <cstring> in your program. Declare your C string character array to be bigger than a postal code, e.g. 20 characters. To input the postal code, use cin.getline(pcode, 20, '\n'); (assuming "pcode" is the name of the array and "20" is the size).

The following is what is expected as output:

hercules[6]% postal
Please enter your Postal Code: S4S 4S4
Valid Postal Code
Please enter your Postal Code: AbC 123
Not a Valid Postal Code 
Please enter your Postal Code: S4S4S4
Not a Valid Postal Code
Please enter your Postal Code: 123456
Not a Valid Postal Code 
Please enter your Postal Code: ABC DEF
Not a Valid Postal Code
Please enter your Postal Code: S4S 4S4a
Not a Valid Postal Code 
Please enter your Postal Code: Abc 4S4
Not a Valid Postal Code
Please enter your Postal Code: S4S 123
Not a Valid Postal Code
Please enter your Postal Code: S4Sb4S4
Not a Valid Postal Code
Please enter your Postal Code: S0G 4A0
Valid Postal Code 

Please test all of the above cases before doing Part III. The following link is a text file with all of these test cases: test.txt. You can copy this file into your directory with the following command:

cp /net/data/ftp/pub/class/115/05-strings/test.txt test.txt
Remember, how to compile and then us this as input into your program:
g++ -o postal mycode.cpp
./postal < test.txt

Part III - Manipulating Strings with C++ Strings and Functions

Copy that program you just created to a new name and use the new file to repeat the postal code verification using C++ strings and functions.

This time use #include <string> in your program. To input the postal code, use getline(cin, pcode); (assuming "pcode" is the name of the array). Remember that the functions for C++ strings are not the same as those for C-strings, so you'll have to change that function check for the length of the postal code.

You might be surprised that you can leave your calls to isalpha and isdigit as is. e.g. isalpha(pcode[0]) Even though you are now using a string type variable, you can still use a subscript to access an individual character in that string because the variable is actually stored internally as a character array.


This page last modified:
Wednesday, 29-Feb-2012 12:03:24 CST

CS Dept Home Page
CS Dept Class Files
CS115 Lab Files

Copyright: Department of Computer Science, University of Regina.