Remember, when the lab-book mentions a program, say, CP09E03,
don't type it in! Copy it to your current directory:
cp ~cot3002l/src/CP09E03.cpp .
This means the C files are in the subdirectory named 'src' in the directory
of the user-name 'cot3002l' (note that's an 'ell' at the end not a one!)
Don't forget the final '.' after the name of the C file. (This means the
target of the copy command is the current directory, and just use the same
name as in the source directory.)
Part One: Exercises on strings in C:
Part Two: Exercises on strings in C++:
Following this link
to similar exercises on the standard ISO/ANSI string library for C++.
This is the one you'll want to use in your C++ programs. Answer the
questions to the you find there and turn them in by email along with your
answers to the above questions.
If you don't finish all of these, be sure to e-mail what you do have by the deadline: 8 p.m. two days after your lab meets.
When you are done with the Experiments, turn-in your work by sending it to the lab's instructor using the e-mail address he or she gives you in class. Be sure to put your name (and your lab partner, if you have one) at the top. Also be sure to number or label your answers, e.g. "Exp. 9.2, Step 1", in the file.
Remember the command to compile your program is:
g++ -Wall prog_name.cc
Always use the -Wall option with g++ to show all warnings,
some of which might be errors. You could also use the -o option to rename
the output file to be something other than a.out
Is the array numbers really a string? No, it isn't a valid string. If you don't know why it isn't, go back and study (again) the explanation on page 136 in the lab book.
When you write an array of characters to cout, C++ assumes that the argument is a valid string. So it will print character-by-character starting at the first until it hits the end of string marker, the null character, '\0'. By good fortune (sort of), the next character after the '5' is a zero, which is the same as the null character. This is only because the UNIX operating system initializes memory to zero's before giving it to you for your program to run.
Again, if you ran this on a PC, you might see something entirely different than what you see on UNIX. The last output statement would print characters until it hit a null character, which might be a lot of characters. It might run into protected memory before that, producing a "segmentation" or "protection" violation.
In summary: Always make sure your arrays of characters are really valid strings! Or, use the C++ string class!