Introduction
In the previous tutorial, we talked about reading and writing binary data to a file.
In this tutorial, we are going to talk about strings. Strings are a way of storing and manipulating text within C++.
Code
Let’s jump right in and create a string:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string helloRpi("Hello Raspberry Pi");
cout << helloRpi << endl;
return 0;
}
As you can probably guess from the code sample above, string is a class that is defined in the <string> library. (The parenthesis after the variable name was the hint that makes me guess that string is a class.) Another way to create the string would be like this:
string helloRpi = "Hello Raspberry Pi";
Since string is a class, it has many methods on it that help manipulate the string. One such method is the substr, which grabs a piece of the original string. So, if we wanted to extract the word hello from the previous phrase we could do this:
string hello = helloRpi.substr(0, 5);
We can also compare two strings to see if they are equal. Take the following code example:
int main()
{
string hello1 = "Hello";
string hello2 = "hello";
if (hello1 == hello2)
{
cout << "Equal" << endl;
}
else
{
cout << "NOT Equal" << endl;
}
return 0;
}
Do you think that hello1 and hello2 are equal to one another? They are not equal to one another because hello1 has a capital H and hello2 has a lowercase h. Since they aren’t exactly the same, they aren’t considered equal.
This brings up an interesting question. If they aren’t equal, than which one comes first? Meaning that if I did a less than operation on them, which one has a “lower” value? The code looks like this:
if (hello1 < hello2)
{
cout << hello1 << ", " << hello2 << endl;
}
else
{
cout << hello2 << ", " << hello1 << endl;
}
When you run the following code, the capital Hello is first, followed by the lowercase hello. Why is that? The answer goes back to the ASCII table. In order to determine which word comes first, each character is compared one by one. If the characters are the same then it moves to the next one. If they are different, the word that corresponds to the character with the lower ASCII value is the lower one. Since H has an ASCII value of 72 and h has an ASCII value of 104 that means that Hello comes before hello. This comes in handy when trying to alphabetize a list of words. Always remember that the default comparisons put the words in ASCII order, which isn’t necessarily alphabetical order, because all capitalized words will come before all lowercase words.
Another common thing to do is to append one string to another (sometimes called concatenation). This can be done using the append method:
string foo = "foo";
string bar = "bar";
string cat = foo.append(bar);
cout << cat << endl;
Or by simply adding the strings together:
string foo = "foo";
string bar = "bar";
string cat = foo + bar;
cout << cat << endl;
Ok, now for a little fun. Using what we know about stings, we can create a simple program that answers the fundamental question: why? Here’s what the code looks like:
#include <iostream>
#include <string>
#include <ctime>
using namespace std;
int numSubjects = 4;
string subjects[] = { "The universe",
"I",
"The man in the white hat",
"A garden gnome" };
int numVerbs = 2;
string verbs[] = { "wants",
"loves" };
int numComplements = 4;
string complements[] = { "string cheese",
"cake",
"bicycles",
"a Raspberry Pi" };
int numCanned = 5;
string canned[] = { "Why not?",
"I said so",
"C++ is the greatest language of all time",
"That is how he wants it",
"Boom goes the dynamite" };
string why()
{
int chance = rand() % 10;
string sentence;
if(chance < 7)
{
// Random
string subject = subjects[rand() % numSubjects];
string verb = verbs[rand() % numVerbs];
string complement = complements[rand() % numComplements];
sentence = subject + " " + verb + " " + complement;
}
else
{
sentence = canned[rand() % numCanned];
}
return sentence;
}
int main()
{
srand((unsigned int) time(NULL));
cout << why() << endl;
return 0;
}
The why function randomly generates a simple sentence as the answer to the question. Feel free to tinker with it and see what crazy answers you can get it to produce!
Summary
In this tutorial, we talked about strings, which are a way to store text in a program. We also discussed ways to manipulate strings, as well as created a small program to answer the age old question of why.
In the next tutorial, we will be discussing constants. Constants are a way of making sure that the value of something never changes. Appropriate use of constants is one of the topics that separates a good programmer from a great one in my opinion.
If you have any questions or comments about what was covered here, post them to the comments. I watch them closely and will respond and try to help you out.