- The standard C++ library
provides a number of functions for working with C strings.
These are different and distinct
from the C++ STL string class
C strings are simple, efficient and flexible
and are often used where the complexity
and power of an object oriented string class
is not necessary.
These functions work on C strings
and are found in the C string header.
Here is string.cpp from chapter eight of the exercise files,
when you'll notice that
I include the C string header here up at the top,
this is where the string functions are declared.
We've also got some constants,
a maxbuf size of 128,
a couple of string constant string one and string two.
And then we have a couple of character buffers sd1 and sd2,
inside of main, along with an integer,
a character and a character pointer,
which we we'll use for various purposes
in our demonstrations.
First up is strncpy
And you'll notice that this has an n
in the middle of the name.
Many of these functions have this n
in the middle of their names.
This means that the function
uses a count argument to limit the number of characters
upon which we will operate.
There are often other versions of the same functions
that do not have the n in their names.
Try to favor the version with the n
as is usually more stable and safe.
So strncpy is used to copy a string
from one place to another.
And here we're copying the constant string
s1 into the buffer sd1
and we're using maxbuf as the count,
it will copy up to maxbuf minus one characters,
and we do the same with sd2.
And if I build and run, we can follow along here.
And you see at the top of our results,
here is our printfs' sd1 and sd2 is string one and string 2.
next is strncat for concatenating strings.
First we can concatenate a space dash space string into sd1
and then we can concatenate sd2 on top of that.
And each time we're using as our count,
the length of the maxbuf
minus the length of the string minus one.
And so we do this in both cases
and that's the length of the destination string
so that we know that there's enough room
in there for the concatenation.
And you notice our result here,
sd1 is now string one dash string two
with the spaces around the dash
string length, strlen, will get the length of a string
and again, it uses the count maxbuf
so that it doesn't count past the end of a buffer
strnlen returns a size t value.
And so you'll notice in our printf
we're using these zd for the value.
Strcmp, for comparing strings.
This returns a one if the comparison
is greater than a minus one,
if it's less than and a zero, if the strings are identical.
And so here we compare them
and you notice that we get a result of
not equals for sd1 and sd2, and the value is minus five.
And we have a zero for comparing sd2 to s2
because they are identical.
Strchr is for finding a character in a string.
And here we set our character variable to letter n
and we search for the n in sd1.
And did we find an n?
Yes, and strchr returns the position
of the first matching character or no.
And there's also a version that searches from the right,
which is strchr with two rs' in the middle there.
And there's strstr
which searches for a string in a string.
And you notice down here,
we're looking for s2 in sd1.
And of course, yes, we find it
and we find it at position 13.
And again strstr returns the position of the string
or not if it's not found
many systems have non-standard extensions to this function
so be sure to consult your system documentation,
even if you're using C++
you should never be afraid to use C strings.
There are times when they're necessary
and there's times when they're just smaller and faster
than C++ STL strings.
These functions are a great resource
for working with C strings.