Hello, and welcome back. Another exciting lecture. In this lecture, we're going to talk
more about strings and string functions,
mainly the ability to search tokenize and analyze strings.
So let's discuss some more string functions. Searching a string.
Searching a string.
All these functions are going to be in the string header.
There's some string searching functions that are particular use
where you can find a specific character or a substring.
The name of those functions
are strchr and strstr.
Finding a single character or substring inside of a string.
We'll provide some examples of that.
There's also some capabilities for tokenizing a string.
You may say what is tokenizing a string. First, you have to define what a token is.
A token is a sequence of characters within a string that's bounded by a delimiter.
A delimiter is something that is in the string that you
notice that has special meaning.
So a token would be if you had a complete string,
this is Jason and it was separated by spaces.
One token could be this, another could be is, another could be Jason.
It's a way of taking an entire string and breaking it apart in certain
boundaries based on a delimiter.
And you want to make sure that your delimiter
is not something that is valid in the actual string. So you also use delimiters when you
read from files, same type of concept.
Where you break a sentence into words, that's called tokenizing. We're going to use
this function called strtok.
I also want to talk about some analyzing strings. There's a ton of these,
many different ones to check whether or not characters
are lowercase characters or uppercase characters
or alphanumeric or digits.
These are mainly done on characters, but characters in side strings
and you can also apply them to entire strings.
Now we're getting into some more details about strings.
And in order to understand some of these functions, we have to talk a little bit about pointers.
Pointers are a huge concept in c.
We're going to have an entire section devoted to pointers.
Probably, the next one or the one after.
And it's a lot of discussion. It's probably the most confusing topic of the language.
I don't want to go over that in detail right now because it's not relevant.
I only want to touch on it, just give you a little sneak peek about what a pointer is
so that you can understand some of these functions that we're going to use.
That's the only purpose of this. So if you don't fully understand pointers at this point
after this couple slides, do not worry.
So the concept of a pointer, again, we're going to discuss in detail later.
But in order to understand some of these string functions, I want to give you a quick peek.
C provides a remarkable useful type of variable, it's called a pointer.
I've been talking about it. It has to do with memory, and memory allocation.
Some languages if you've programmed before and you're not entirely new to
programming, some languages will automatically manage memory for you.
They'll allocate memory for you and delete it, language like java does this.
It's called the garbage collector. A language like c does not do this.
So whenever you create memory for
using pointers, you have to delete that memory.
But what a pointer is it's very useful
and what it is it's a variable that stores an address.
An address, remember, is something in memory.
It's usually hexadecimal.
The value in the address is in another location in memory.
The pointer just points to an address that address contains a value.
And we've actually used addresses in the past with the scanf function.
Remember, that ampersand symbol that we've used that's the address of operator.
Scanf function as its second parameter
would have to take in a variable that you wanted to store the data,
that variable it has to be a pointer.
And so the scanf function expected a pointer.
To create an integer by itself, it's not a pointer,
but if you get the address of it, it now becomes a pointer.
So we're using the ampersand to get the address of a primitive data type.
So you're a little familiar with the address concept.
Remember, from now with pointers,
you create int number
and that's not a pointer and it's equal to 25.
But if you get the ampersand of it, which is the address of it, you can then assign it to a pointer.
A Pointer has special syntax in c, the asterisk represents a pointer.
So we declared a variable number with the value 25 and then we declared a pointer
using the asterisk named pNumber,
which contains the address of number.
And again, the asterisk is used to declare the pointer,
that's how you know it's a pointer data type.
To get the value of the variable p number
because p number is what? It's a pointer it's pointing to an address in memory.
If you just try to look at the the value of p number, it's going to give you the address.
If you want to get the value of what the address is pointing to, you have to de-reference it.
So there's special syntax
that c uses it's called the asterisk and the name of the variable,
that's how you can actually access the memory location that contains the value.
So if you want to assign data to that memory location, you have to say star p number.
Again the effect is it accesses the data stored at the address specified by the pointer.
So it becomes very confusing when we talk about pointers because you're not used to this concept.
You think a variable has a value, that's it. But a variable can have an address
and the address can then have a value.
This slide tries to illustrate what I just mentioned.
I found this in a book that I've read.
It's a slide that shows you the concept of a pointer in memory
and how things work with addresses.
So I'm kind of reiterating what I just mentioned.
But the value of ampersand number is the address where number is located.
So if you have two variables, number
and p number, p number is a pointer, number is an int,
you also, by the way, have to specify the type of the pointer.
Even though pointer is an address,
you still specify the type or where the address is pointing to.
So int star p number is going to be equal the address of number.
Number is in memory,
and it points to an address of a thousand for example and it has a value of 25.
P number has a
address of 105 for that actual p number,
but the value of that address is 1000 address.
In order to get the value of p number, you would have to dereference the 1000,
which will give you 25.
The reason I mention all this thing about pointers
because a lot of string functions are going to return pointers.
It's not going to really matter that much.
It's only important when you want to understand how you're getting a certain character inside of an array.
But I did want to briefly mention them.
It's okay if the concept doesn't sink in right now because we're going to cover it
in a ton of detail in a later section.
But understand that the string functions now that we're going to use are going to return pointers
and then you're going to maybe use that return to do something useful.
In the past when we use the str copies and the str compares,
we passed in pointers but it never returned pointers, we never had to use that pointer.
Now that we have to use the pointer, I want to just give a little description of what that means.
Pointer is just going to point to an address.
And then you can use that address to do parsing or whatever you need to do.
So hopefully, this didn't overwhelm you. Again, don't worry if you don't fully understand it.
We'll go through some examples now of
analyzing, tokenizing and searching strings.
To search for a string, we mentioned, you're going to use the strchr or the strstr.
The strchr searches a given string for a single character.
The first argument to the function is the string to be searched,
which will be the address of a character array.
The second argument is the character that you are looking for.
We'll provide an example in a second. So the function is going to search
the string starting at the beginning
and return a pointer to the first position in the string where the character is found.
This is why pointer is important now because we're not returning
an entire value, we're returning a pointer to a
character inside of a string
and that address represents the index of an array, essentially.
So the address of this position in memory, it's a type character star
and it's described as a pointer to a char,
which is really just a single character in memory.
To store the value that's returned, you must create a variable that can store the address of a character
so you have to create a pointer.
If the character is now found, the function returns a special value, which is null.
Null means that the pointer doesn't point to anything.
We use null when we talk about pointers,
and null is just saying it didn't find anything. So it's not pointing to anything.
When it points to something, it points to the address of where you found that character
and then you can start parsing from there.
Because remember from the earlier lectures,
when you create a character array in memory,
you just have each character represented as a byte.
And if you're parsing a string
and you want to see where a particular character starts in that string,
you return that byte and then you can start looking from there.
So str char, how you use it is this.
You say you create a character string
and we initialize it here with a quick brown fox.
We then create a single character and we initialize that with q.
Then we create a pointer because we have to create a pointer because that's what's returned from the str char,
that's what allows us to see where in that string we found a character.
We're going to initialize our pointer to null. Because right now it's not going to point to anything.
Then we're going to call str char, we're going to pass in our string which is the quick brown fox,
and we're going to pass in our single character which is q.
What's going to happen is it's going to look inside the quick brown fox.
And the first time it finds a q, it's going to return the address of that q.
So p got char is really going to be pointing to
the fourth index of str.
Because t is sub-zero, h is sub-one, e is sub-two, space
is sub-three, q is sub-four.
So p got char is going to point to q in the quick brown fox.
And so you can see exactly where that character starts in that string.
So the first argument to str char is the address of the first location to be searched,
that's the search string.
Second argument is the character that is sought, the character that you want to look for,
a single character.
It expects the second argument to be of type int.
So technically str char, the second argument takes in.
But c compiler will automatically convert the chart to an int.
It will get the ASCII value, essentially.
So you could actually define the q
single character instead of a single char, you could define it as an int.
That's just more of a technicality that I wanted you to know about.
But p got char is going to point to the value quick brown fox
because it's going to start at q and then it's going to go to the null terminator if you try to print it out.
So it's actually going to be quick brown fox when you run this example.
This is how you can search for a single character in an entire string.
Use str char then use the pointer and then display the pointer.
And how do you display that pointer? The same way you display other characters, use printf, percent s.
So this is a nice mechanism to search for characters in an entire string.
If you want to search for a string inside of a string, which would be a substring,
you use strstr.
This is probably the most useful of all searching functions.
It searches one string for the occurrence of a substring.
And it returns a pointer to the position in the first string where the substring is found,
very similar to str char, but it searches an entire string. If it didn't find any match, it returns null.
The first argument is the string that is to be searched just like str char.
Second is the substring you're looking for, another string. So here's an example.
It looks very similar to the other one except instead of looking for a single character,
we're looking for a word, a string.
So we have word, which is equal to dog.
And we have text which is every dog has his day.
We create our pointer again because it has to be - this is what the function returns.
When we call strstr, we pass in the text the string that we want to look at.
And then the second argument is the word that we want to look inside that string.
So it's going to search for the text the first occurrence of the string stored in word.
The string dog appears at the seventh character in text.
If you look at text.
Text sub 6 is going to be equal to dog, that's where it starts.
So the pointer is going to point to the address of text plus 6
dog, and it's going to start there, and then it's going to go to the null terminator.
So p found is going to be dog has his day.
And also be careful because the search is case-sensitive.
If you actually provided a capital D-O-G, you're not going to find anything,
it's only going to look for the correct case.
But this is used to find substrings and strings, very useful because
a lot of times in a program you're going to say does this string inside this string
because it's almost similar like a grep if you've ever used grep on Linux.
I hope this makes sense. Tokenizing a string.
A token is a sequence of characters one of them string that is bound by a delimiter, we know this.
A delimiter can be anything, but it should be something unique to that string.
Don't have a delimiter as "A" because now it's going to find in that
string something valid in a word.
Make it something unique like a space or a comma. This, again, is used as a boundary
for usually words in a single string.
So it breaks a sentence into words, it's called tokenizing.
This strtok function is used for tokenizing a string.
It requires two arguments.
The string to be tokenized, which is the entire string that has the spaces and has the delimiters.
And then the string containing all the positive limited characters.
You can specify what you want to set as the boundary.
So here's an example.
We're creating a character array 80.
It says hello, how are you, your are dash my name is dash Jason.
We then create a constant string that represents the delimiter,
which is the minus, the dash. It looks a little different, but it's still the minus, the dash,
they're both dashes.
And then we create our pointer which is token.
Because strtok is going to return
our each token.
So if we call our strtok and we pass in string
and we pass in our delimiters s,
token is going to be equal to the first thing that it finds.
So the first time you call strtok, it's going to be equal to hello how are you
space and then it finds the match with the dash.
And if you call for a second time,
it's going to print out my name is.
If you call it a third time, it's going to print out Jason
because it's going through the dashes.
So that's why we have a while loop here. We say while the token is not equal to null,
print out the token.
And then we get the next token by calling strtok.
And we say null as our first parameter
and s is our second parameter
and then we, we display. So let's look at this in
code blocks since it'll make it more understandable.
So let's build this. We just went through this example.
I want to show you what the output is, so it's clear what's going on here.
So here you'll see that it says it
prints out the first token which is hello how are you.
Well, the first thing it does is it
finds the first token, prints it out, hello how are you, my name is and then Jason.
So now we have hello how are you,
finds the first dash, prints it out. My name is finds the next token
then the third one is Jason. But you have to call the token
three times to get each token.
The first one is going to tokenize on the str.
See the next one you can pass in null because you don't want to look again, you just want to get the next token.
And so you pass a null the second one and you just pass an s.
It will give you the next token of the str already tok.
So that's how you can use strtok. It helps a lot with parsing.
You can parse something and get certain boundaries out.
Last functions I want to talk about are these analyzing string functions.
There's a ton of them, so I just provided a little table here.
Is lower, is upper. It does exactly what you think it would do.
It tests to see if a character is lower case or upper case.
We're testing certain characters, but they're used inside strings.
And so the argument to each of these functions is the character that you want to look for.
And then all these functions return a non-zero value of type
int if the character is within the set that's being tested.
So we're going to pass int characters inside looking for inside words.
If we have an uppercase letter inside of a word, it will tell us
and we can use it for parsing individual elements in a character way as well.
So the return values will convert to true or false
so you can use them as Booleans as well, which is nice.
So is lower will return true
if a word or character has a lowercase letter.
Well, here's a nice example I wanted to run through,
which will count the number of letters digits in an entire string.
So let's run this because this will be clear, but what
you should notice is
we're creating a buffer, our input buffer and then we're creating three integers to count.
We're telling the user to input
a string of less than so many characters, we then read it in.
We're then going to look at that string to see if it contains
certain characters.
And so we're going to iterate through that string
by looking at each index in the array.
So here's our example.
Again, buffer is our character array that's going
to store inputs. We're going to use scanf to populate that.
We have to type in our string with no spaces, it's a single string.
We're going to run through it, go through each element in the buffer and then call the
is alpha, is digit or is punct functions to see
if those characters are in there,
and then it will increment based on that whether or not that returns true or false
and then it will display out how many letters, digits and punctuation characters are inside that string.
Let's run this.
I'm just going to enter something with a bunch of uppercase,
maybe some digits.
Then when I hit return, it's going to count everything. So it says
my steering contained 24 letters, 4 digits and 1 punctuation character,
the colon.
Nice little neat little program that utilizes those
character functions but are useful with um
analyzing a string because that's what you're doing, you're analyzing the string,
but you're just going through character by character. Thank you.