- A string is a funny thing.
It's not really a C language variable,
but it's used that way.
And, when it comes to manipulating strings in your code,
you have to be really careful due to their unusual nature.
This movie discusses working with strings.
I'll show you how to gather information about a string,
stick strings together, compare strings,
and find one string inside another.
These tasks represent the more popular
string manipulation functions in the C language.
A string is declared at line six.
It's a character array,
so the char variable type is specified.
And the variable name is string, followed by empty brackets.
The string length function, strlen, appears at line nine.
I pronounce it "stringlen" because it means string length.
It returns the number of characters in a string,
which here is stored in the len variable.
Build and run this code.
The string is 19 characters long.
You can also count the characters in the source code,
but you won't always have the luxury
of knowing the exact amount.
For example, what if the user types in a string?
Open exercise stringmanipulation2.
This source code file is based on an exercise
from an earlier movie.
Input is fetched by using the fgets function,
and then it's stored in an input buffer.
Afterwards, the strlen function
fetches the length of the string.
The next statement displays the results.
Build and run.
I'll type.
And apparently, that string is 20 characters long.
Keep in mind that strings in C end with a null character.
Storage must be allocated for that extra character.
The program generates the character,
but you must provide room for it in the input buffer.
And the string length function does not count that character
when it measures the string's length.
Open exercise file stringmanipulation3.
This is only partial code.
You and I are going to figure out
how to stick these two strings, first and second, together
to make another, new, string.
In may programming languages,
the addition operator does the trick.
For example, would that work?
Save, and then click the Build button to find out.
Well, that's a no.
You could display both strings back-to-back,
but that's not the solution I'm looking for.
What you need to do is to stick the strings together.
Now, rather than say, "Stick the strings together,"
C programmers say, "Concatenate," which comes from
the Latin word for 'sticking two strings together.'
But, before then, fix up the code.
First, create a storage location, or a buffer,
for the resulting two strings.
I'm just guessing it's 64.
I could count each string
and see how exactly big they both are,
then add one for the null character
and set the value that way.
But 64 is a holy computer number,
so I'm using it instead.
The first thing I'm going to do is to copy the string first
into that storage container.
The strcpy, or string copy, function
copies one string into another.
Here, the string first is duplicated
into the storage buffer.
And I need to prototype that function.
All string functions are declared
inside the string.h header file.
The next thing to do is to stick the string second
onto the end of the storage buffer.
This statement tacks the string second
onto the end of whatever's in the storage buffer.
Concatenation.
And then, finally, display the results.
Save it.
Build and run.
And it worked!
Con - cat - enation.
Another popular string function compares two strings.
This code uses the strcmp, or string compare, function
to compare two strings.
The value returned is zero when both strings are the same.
That's one of those weird times
where zero actually means 'true.'
Values less than or greater than zero
indicate incorrect matches.
Here, you'll also notice that the scanf function
limits input to only eight characters.
The input matches the eight-character limitation
on the buffer.
The buffer is nine characters,
to account for the null character at the end of the string.
Build and run the code.
Now, try a few passwords before you type the real one,
which is 'secret.'
Okay.
Yes.
By the way, this code doesn't need the compare variable.
You can remove it.
Delete it thrice from the code
and replace it with the function directly.
Place the string compare function
inside of the if statement.
Save the changes.
Build and run.
The final string function I'd like to show
is called string string.
It's used to find one string inside of another.
Open exercise file stringmanipulation5.
This code uses the strstr function,
which I call "string string."
It's a little weird because you really need to know
more about pointers to effectively use this function.
That knowledge is gleaned in a later movie.
For now, the strstr function is included
as part of the if statement at line nine.
It returns a pointer, or the location of the string find_me
in the string source_string.
If no match is made, then the value null is returned.
Again, null is a pointer thing.
Build and run the code.
And the string is found.
You can alter the text for the string find_me
to prove that the function actually works.
And it does.
Until you breach the rocky shoals of pointer land,
that's about the best that can be done
with the strstr function.