Last time, we learned that strings in C are arrays of characters and well-formed strings in C are terminated by the null or /0 character. This time, we'll learn how we can find the length of a string at run time. Okay, let's start by declaring our message string. And this time I'll use a different string Def Leppard rocks. Of course, this is not how you spell Def Leopard, so there I've fixed it. Let's count, and print the length of the string. The way we'll do this is we'll declare a variable that will keep track of how many characters we found so far, and then do a while loop. And we'll say while message i, because as you may have noticed last time, we can index into a string just like we index into an array because a string is just an array of characters. While message i not equal to that null terminator. I will just increment i. And you'll notice Visual Studio fought back and said, no you can't space it the way you're going to so I'll just leave that alone. Basically what happens is we just keep moving along in the message array of characters, the message string until we find that null terminator. At that point, when we get here, i is exactly the number of characters in the string not including the null terminator. We typically don't include the null terminator. So we can print, And we'll say the message has %d characters. And of course, there's two things I need here, message and i. Let's go ahead and run it, and there you go, Def Leopard rocks has has 18 characters. Feel free to count them if you want, but that is the correct answer. Now, you have to imagine though that the strings are pretty commonly used in C and they are. So you must believe that there is in fact some function in the C standard library that will measure the length of a string for us, and of course you're right. Let's go take a look, here's our old friend, the C standard library in Wikipedia. If we look at the header files, we see that there is in fact a string.h, I will click on the string.h. And if we look at the string examination stuff, we'll see that there is in fact a string length function that returns the length of the string. We're going to have to chase it down by clicking the reference and then clicking the link in the reference. And you'll see that we can call string length and we pass in essentially, the name of the string variable. And it's going to return something to us of type size_t. And we'll see how to output that type size_t soon. We'll also talk, I will just minimize this we'll go back to our code and use it. And then we'll come back here and talk a little bit about this other optional function. So let's print the string length, Using function. And I'm just going to grab this and bring it down here. I'll change i to string length message. And because I'm using string length here I needed to pound include string.h up here. The other thing that I want to do, even though the Visual Studio compiler doesn't complain is, string length as we discussed, returns size_t. And the format specifier for size_t is zu. So let's Ctrl+F5 and as you can see we get the same exact answer. Now there is a problem with using strlen or using the approach we used up here. And the problem is if we have a malformed string, a string that is not null terminated, then we could end up in an infinite loop up here. And this would return an incorrect value and in fact, the fact that there is this vulnerability in string length has led to a variety of cyber attacks on C programs so we would like to avoid that. Let's go back and look at the C standard library documentation again. We can see that there is this other function, where we pass in a size as well to make sure you can't do that. So for this function, we'd pass in the string and pass on the size of the string. And that would really be the size of the array, so if the string was smaller than the array and null terminated, you'd get the right length. If the string was the size of the array, but not null terminated, you would get the size of the array back. But it won't keep racing past the end of the string looking for a null character that's not there. So we're excited about this strlen_s. So let's try it. And remember we provide two arguments, we provide the string and we provide the size of the string. And now when we compile, we get a build succeeded and that's great, that's very exciting. The bad news is that if we use the same exact code in XCode, it won't actually compile for us. Let me show you what we can do, I'll get rid of the _s. I'll compile again, I still can compile. Notice this is not strlen, there's an n right there in the middle. And this will work on both Visual Studio and on Xcode. I'll run it, so we still get the correct result. And we get the safety of not running past the size of whatever string we're passing in. To recap, in this lecture, we saw various ways that we could find the length of a string. And we learned that using the strlen function with the n in the middle is the safest way to determine the length of a string at run time.