C++ Lesson 13.2 – String and Character Manipulation

In this lesson we’re gonna go over string and
character manipulation. This is a complex and
sometimes confusing topic. Well, we’re gonna look at two kinds of strings in
this lesson. We’re gonna look at the standard string class type object, strings that you
learned at the beginning of semester. It also
known as the standard string class or C++ standard library strings. And we’re gonna
look at the c-style strings or c-strings or what I
prefer is null-terminated character arrays or NTCAs. So as
an example, “char name[10]”, initialized to
“Clayton”. That is a c-string or null-terminated character array. And
alternatively, we can create a string type object “name” and initialized to Clayton. Now, I’ve
included the library “string” and that gives you the ability use some
built in functions that the string class has. Now
there are lots of functions and some implementations, include more functions than
others. You may read of functions in texts that we
do not have with our system. I wish I could give you a complete list, but I don’t know what they are
either. In any case, if you see reference to a string object function, don’t assume that our compiler
owns it. And using the standard string will also
identify for the compiler what string refers to. You can say
using namespace standard also and that will
work. Referring to the standard string, suppose that we
declare “name” to be a string and give it a value
of “Clayton”. I want to output “name.length”. Now, clearly, this is a function call because of the
parenthesis. What kind of function is this? Notice
the dot operator. You’ve seen this before with structs. What you
are doing here is calling a member function of the class of objects to which “name” belongs. That is the string class. “name” is an object of
type string. It can call a member function of the
string class. We’re gonna take a look at this notion in more depth when we get into the object
oriented programming part of the course in later
lessons. Let’s see what we get. Quite obviously you would think that this is gonna
return the length of the data in that object and sure enough it does. Man, that’s enough to make
you sick. Let’s go on. Well suppose the user enters “Pattie Boyd”. What happens? Well, upon output, it outputs only “Pattie”. What
happened to the last part of that? Well, something
you have to be aware of is that the extraction operator, used for a string type object will only
read up to the first white space. So, when reading in “Pattie Boyd”, it reads up to this
space and that’s it. The “Boyd” remains in the
input buffer. It’s there you, have to remember that it’s there. You have to always be
aware of what’s in your input buffer. The “Boyd” is
placed into the variable “name” and that is what gets output. More limitations. Suppose we have “string
name;”, “string hometown;”, prompt for your
name read it in, then prompt for “hometown”. Okay, what’s going on here? So, you get the
prompt “What’s your name?” Let’s suppose you enter “James Marshall”. What happens? Well, “James Marshall” goes into
the input buffer “James” is copied from the buffer into the variable
“name”, “Marshall” remains out there in the buffer, then
you get a prompt for your hometown. What
happens then? The system does not wait for the user input. You don’t get a chance to enter
anything at the keyboard. Marshall is going to be
pulled off of the buffer and placed into that variable “hometown”. You didn’t have any choice.
So, sometimes you’re going to get very funny
behavior out of your code when using strings and the extraction operator and the getline. Okay,
let’s take a look at the fix. We’re going to
introduce you to the getline function. There are 2 versions of the getline function, one for standard
string object and one for c-string object. the
parameters are an input stream, the string variable you’re going to place that
information into, and a delimiting character. You
can make that character any character you want. You can read up to the first instance of an ‘!’ or a
‘.’ or the new line character or a ‘B’ or anything.
So, how do we use it? Well, let’s take a look. We have two string
variables “name” and “hometown”. So let’s match
our getline with the template here.


Add a Comment

Your email address will not be published. Required fields are marked *