Tuesday, July 27, 2010

C++: Examining string of characters

By now, you would know how functions work in basic terms - how to declare
it, arguments and concept of return values. In this part, we'll examine more
details about functions by looking at what we can do with string variables.

There are a number of reasons why I chose strings for this series. First, as
you might have guessed, a string is essentially an array of characters.
Second, there are a host of functions we can use to manipulate strings,
including finding an exact character within a string, taking a part of a
string and assigning it into a new variable and so forth. Third, using these
functions, I can gradually introduce you to the concept of classes, objects
and member functions, as you'll see after this chapter.

So, as you have seen, we declare a string just like any variable:
string var; // Where var is the name of our string variable.
However, as you've seen, in order for us to work with strings, we need to:
#include <string>
Since "string" is not a built-in data type (although it is an array of chars
though). The string library, in addition to allowing us to declare string
variables, hosts a number of "helpers," or member functions. Let us meet our
helpers, shall we?

Sometimes, you may need to take part of a string and assign it to something
else - perhaps for comparing two strings or printing just a portion of that
string. To do this, we use a "substring" function (substr) to take part of a
string and use it for other purposes. The syntax is:
string_var.substr(start_of_selection, length_of_string);
Where "string-var" is our string variable, "start_of_selection" is where we
want to start the substring routine e.g. where a portion of our string
begins, and "length' is the number of characters that the substring should
"capture" e.g. set aside for further action. Note that the first character
in a string starts at zero (0.
For example, say if I have a string variable with the following string:
string text = "Welcome to Blogger.";
The total length of our string is nineteen characters, so the length range
would be from zero to eighteen. Suppose if we want to set the word "welcome"
aside for another string variable named "intro." We can do the following (in
English):
We want to take a portion of our string, so we need substr function. The
start of our portion is at zero (at the letter "W") and will run for seven
characters (welcome). Using what we've just obtained, assign this word to
another string variable.
In C++, it goes something like:
string intro = text.substr(0, 7);
In other words, we've just taken the first seven characters on our first
string (under text variable) and assigned it int "intro" one. In terms of
arguments, the start of substring could be anywhere - at the start of our
text (zero) or even halfway across. As for length, it could be any length,
provided that it does not exceed the total length of our source text (we'll
see how we do this below). For example, if we want to select the word
"Blogger" from our text above, we could say (assigning the substring to blog
variable):
string blog = text.substr(11);
Notice that I only put one argument, and that is perfectly legal. What we
have just performed was select from the letter "B" to the end of our string.
So, if we output our blog variable:
cout << blog << endl;
It'll print, "Blogger."

But how do we know the length of our string? The next friend we'll meet is
the length function. This just tells (returns) the length of our string. Let
us return to our message under "text" variable. If we use the code:
size_t length = text.length();
Few things here: don't worry about size_t, it's just an enhanced version of
integer variable used to store size of a string or similar things. The
length function did not take any arguments (and that is legal); what it will
do is just tell us how many characters (technically elements) does our array
of characters (string) has - in our case, 19.

Another quick visit to another friend and I'll let you go, and that is the
question of how we find a specific character in our string. Logically
enough, this friend's name is find function, which it does what it is
supposed to do: find a character for us. All we need to put in this member
function is just one argument: the actual character we are looking for. What
will it give us in return? The location where this item is located, if it
can find it.
For instance, using our text variable, suppose you want to find the first
instance of the letter "g". We would write:
Text.find("g");
Or something similar. Be sure to put quotes around the character. Luckily
for us, there is a "g" - it's in location 14.
As a side note, we can use this function to find anything - even a word.
What it will return is the location where the start of our search string is
located. (a teaser: this fact becomes very important much later...)

I think that's enough for now. Hope you understood the concept of a member
function and examples of its use in string library. In the next post, we'll
meet more member function friends, this time the ones assisting vectors.

P.S. Hope this was simple for you. I apologize for any errors or
misinformation on this post...
// JL

No comments:

Post a Comment