Section 9.1 of Absolute C++.
Let's start with a simple problem: Write a program that reads firstname lastname, and prints out lastname, firstname. For example, a typical run of the program might look like:
... where the red text is user input. This should be no problem, something like this will do:
string first, last;
cin >> first >> last;
cout << last << ", " << first << endl;
Now let's make things more difficult: Suppose I
also want to capitalize all the letters in the names. No matter how hard you
work with what we've learned of C++ so far in this course, there's no way you
can write this program! You can capitalize, you can switch first and last
names, you just can't do both together. The problem is that you need to access
the characters within the strings
last, and you need to
know how many characters are in the strings.
The "how many characters are in the
strings" part is easy. It turns out that whenever you have an object of
type string you can put a
.length() at the end of it and the resulting
expression returns an int that equals the length of
the string. So, from the previous code I could ask for
and (with first name
"Chris") the result would be 5.
As you might suspect, it is also possible to
reference characters within a string - not by specific names, but by indices.
So the initial character of the string
for example, has index 0. To reference it, say for printing, you write
0], which we usually read as "first
sub zero". Characters within a string are thus indexed from zero up to one
less than the length of the string, like this:
Using indices, we can make sure that every character within the string
first is printed in capitals:
for(int i = 0; i < first.length(); i++)
if ('a' <= first[i] && first[i] <= 'z')
cout << char(first[i] - ('a' - 'A'));
cout << first[i];
In fact, it might be kind of nice to make a function to do the printing in capitals for us, and produce a program like this.
When we access an ordered collection of objects,
all of the same type, by indices rather than by individual names, we are using arrays.
This is one of the most powerful ideas in computer science, so you better learn
to love it! The exercises below and the remainder of the notes here are just to
help you play with the idea of accessing objects within an array by
index rather than by specific name. A string is, essentially, an array of objects
char. In coming
lectures, you'll see that we can have arrays of any type of object we choose.
Arrays allow us to store and compute with large
amounts of information in programs without needing separate variable names for
each object we store. In the past example, a single variable name
first stood for as many
char objects as needed.
The key behind this indexing with strings, is that
is an object of type
You don't need any special rules to tell you what you can and can't do with an
because anything you can do with a
you can do with it. The one potential pitfall is this: Don't use a negative
number for an index, and don't use a number that is greater than or equal to
the length of the string! There aren't any characters there to index!
Now, we might want to actually change the string we've got - for example, modify it so that all of its letters are capitals. We can do this the following way:
cin >> s;
for(int i = 0; i < s.length(); i++)
if ('a' <= s[i] && s[i] <= 'z')
s[i] = s[i] - ('a' - 'A');
cout << s << endl;
You say "How do I know I can assign a value to
like that?" Well,
s[i] is a
like any other so, you can assign to it just like you assign to any other
Last modified by LT M. Johnson 10/12/2007 03:41:37 PM