Home Classroom C++ C++ with Barney! Class 11

C++ with Barney! Class 11

1132

Strings

[You're Ted and you decide to learn some C++ from your friend Barney. This is the eleventh day of the class]

You: Hi, Barney!

Barney: Hi, Ted! You’re real late today.

You: Got a little late. Life’s problems, you know.

Barney: That ain’t a good excuse. Never mind. So what were we doing last time?

You: Mm, we talked about character sequences and you told that we’ll be learning about strings today.

Barney: Fine. First of all, do you know what a string is?

You: A sequence of characters?

Barney: Exactly! The character arrays we talked about last time could also handle such sequences. What do you think is the difference between a normal character array and a string?

You: Mm, not exactly sure, but I think strings are more powerful than usual character arrays.

Barney: Of course. When you deal with arrays, it’s quite difficult to handle them, especially when it comes to characters. The string library has made manipulating character sequences easy. For this, there is a separate data type called ‘string’. Let’s look at an example:

You: So we have to include ‘string’ to use strings?

Barney: Yes, but in some compilers it will work without that too. But it’s safe to include that.

You: Okay.

Barney: As you can see, we have declared a string s and assigned it with the string “Hello there!”. And then we simply printed it. Ain’t that easy?

You: Yeah, so far so good.

Barney: What we saw was a simple string constructor. A constructor is how you initialize a data type, object, etc. There are more than one way of constructing a string, but for the time being, the method we saw will do. Okay, now that we have a string, what kind of things do you think that we may need to do with it?

You: Mmm, to know what the first letter of that string is and to know how many letters there are in that string, etc?

Barney: Yes. For each of the ones you mentioned and many other useful operations there are simple methods in strings. For example, if you need to get the first letter of a string, you consider the string just as an array. So if you want to get the first letter of the string s you would simply..?

You: Type s[0]?

Barney: You’re a real fast learner Ted! Proud of you.

You: Lol

Barney: Right, so you understand that we can access any character in a string like in an array. Now, the second thing you mentioned was finding how many letters there are in a string. For this, the method is simpler. The statement s.length() will give you the length of s.

You: But why the two brackets?

Barney: If you remember correctly, we used such double brackets somewhere else during our journey.

You: Yes, in functions.

Barney: Yes, and there are such functions defined in the string library. Length() is one of them. It returns the length of the string with which you used it. Let’s look at a simple example:

You: I see. It seems strings are very powerful.

Barney: You still haven’t seen the real power of strings. There are several other functions you can use to manipulate strings. Suppose you need to attach one string to the end of another string. What do you think is the way to do that?

You: Mm, supposing the two strings are α and b, we can do something like α=α+b?

Barney: Exactly! It’s as simple as that. Or you can use the append function in the strings library. For example, a.append(b) will do the same trick you just did.

You: I see.

Barney: Now suppose you need to erase some characters off the string. There’s a simple erase function for that. What do you think the statement a.erase(2,5) will do?

You: Erase the characters from 3rd to the 6th?

rd character, 5 characters will be erased.>

You: Ah, okay.

Barney: The command erase has yet another syntax. Suppose you need to erase the characters of the string starting from the 3rd character and until the 3rd from the last. What will you have to do?

You: We can get the length of the string using the length() function we discussed earlier and take away… mmm… 6 from that value and get to know the number of characters we have to delete?

Barney: That’s one possible way. There’s another easier method. You can do the same thing by the statement α.erase(α.begin()+3, α.end()-3).

You: What are those begin and end for?

Barney: They’re called iterators. They give the positions of the first and last characters of the string respectively. So you can understand what that statement does, can’t you?

You: Yes, I get it.

Barney: Good. Now suppose you need to insert some characters into the middle of some string. The function insert comes to the rescue. Its syntax as simple as originalstring.insert(starting pos, string to insert). Let’s look at an example.

As you can imagine, the output of the above is,

You: Hmm, nice.

Barney: So this is only a start. We discussed only a handful of functions we can use to manipulate strings. There’s a lot more functions which you can find in the internet. For example, http://www.cplusplus.com/reference/string/string/ has a listing with explanations and examples. The next time we meet, let’s write a really useful program using what we learned about strings.

You: Okay, thanks a lot. Bye Barney!

Barney: Bye, Ted! By the way, don’t get late the next time!

You: Sure!

Comments

comments

Engineer. Loves tic-tac.

NO COMMENTS

Leave a Reply