Strings and chars
After ints and doubles, our next important variable types are string and char.
We can use a string to store text, and a char to store just one character.
We wrote stringsAndChars.cpp
which demos most of the string functionality we'll need in 10A.
Here's a quick list, with common mistakes in red.
Typing strings and chars in your code:
Be careful with escape characters like \n \"
and remember single quotes (for chars) vs. double quotes (for strings).
Printing: code like cout << myString works the way you'd expect.
Indexing: We can use myString[ 3 ] to access the char in the 3rd slot of myString.
We count slots starting from 0, not 1!
So if the string has 5 characters, the last slot would be myString[ 4 ].
Also, if we try to access a negative index or an index that's past the end of the string,
we'll just get random nonsense. This can crash the program too!
Addition: Adding two strings just "sticks them together" into one big string.
Adding two chars will convert them to ints using ASCII values,
add those ints, and then return the result as an int!
myString.length() is the number of characters in myString.
myString.substr( 3 ) is all the characters of myString from slot 3 till the end.
myString.substr( 3 , 5 ) means the chunk of myString that starts at slot 3 and includes 5 characters total.
If there aren't enough characters, substr will just return a shorter substring.
For example, if myString is "abcdefghi" then myString.substr( 3 , 5 ) is "defgh",
but if myString is "abcdef" then myString.substr( 3 , 5 ) is just "def".
myString.find( "hi" ) finds the first slot in myString where the substring "hi" shows up.
So if myString is "efghijk" then myString.find( "hi" ) would be 3.
If myString is "lmnopq", then myString.find( "hi" ) would return -1, since the substring never appeared!
Strings and chars
We wrote convertToUpperCase.cpp,
which shows some basic string and character operations.
The way C++ deals with adding or subtracting chars (treating them as numbers)
is confusing at first, but it's often really useful!
Here are some Google searches you could try if you're having trouble with today's material.
I'm deliberately trying to choose search terms that you could have thought of yourself!
c++ string or std::string - either of these would help you find
which lists all the functionality of strings.
Near the bottom there's a list of all string functions,
and you can click any function to see examples of how to use it.
In PIC 10A we're only responsible for a few of these functions (see my list above),
but there are a bunch more cool / useful ones!
ascii table - if you want to look at the conversions between chars and ints.
Extra stuff to try for fun (not required)
This is a modified version of the convertToUpperCase.cpp program from class.
Try running this version a few times, and figure out what it does.
Figure out how/why the code works!
It may help if you run the code with some extra printouts to show what's going on.
Note this program includes examples of if statements and for loops.
Note that the version from class relied on the 'magic number' 32,
but this version does not.
That's a useful trick because it means I was able to write this version
without looking up an ASCII table.