Hello, and welcome. In this lecture, I'm going to provide a demonstration of
a solution for our previous challenge.
I'm going to go through how we can
reverse the characters in a string using the str length function
and also go through sorting strings using str compare and
also str copy
as well as using the bubble algorithm.
Let's bring up code blocks.
And I'm going to just create a single program I think for this. I mentioned in the
challenge that you create two programs.
But for simplicity and demonstration-wise, I'm just going to create a single one.
Create a project,
I'm just going to name it string functions
or some variation of that.
Let's call it common string functions
example.
So let's start with the reverse. The reverse can go on its own function or we can just
put it in the main. I was going to put it in the main.
So first thing I want to do is I'm going to create a -- well, first thing I want to do actually is I want
to include the correct header file so that I can use these string functions,
which is my include string.h.
This is where it contains all the nice string functions.
Now I'm going to create a character array. I'm going to make it a size 100.
It's just arbitrary,
just won't be able to store things greater than 100.
I'm going to create a couple local variables
that will help me with my parsing. And what I want to do is I want
to display some output first to the user.
So I'm just going to say a couple line feeds,
print individual, individual
characters of string and reverse order.
That will tell the user what's going on and then also
print out just to make it more apparent,
just print out something like that, so it's easier to read.
I'm going to say input the string. I'm going to read in the string.
And then I'm going to store the string.
So I'm going to say scanf,
present s and I'm going to say str.
And I don't have to pass the ampersand to read it in.
Then I'm going to get the string length. I'm going to put that in my l local variable.
So I can call str link by just saying str length
passing in the string, and it returns how many characters in that string. Very, very easy.
And then I'm going to print out something to tell them that the characters in reverse order.
So the characters of the string in reverse
r backslash n.
Then I'm going to write a nice little for loop that goes through my array
and according to the length and just reverses it.
So I'm going to say for I equals l.
So I'm going to be going backwards in the actual
string array. I'm going to start off at the end and go forward.
And say I is greater than or equal to 0
and I minus minus. And you notice I don't have to look at any
null termination characters when I do it this way.
Then I'm just going to print out each character as I encounter it.
To print out a character and see you just do the percent c.
And just do that and say str sub-I.
And that is basically it.
And then I'll just print out a new line.
And actually I'll print out a bunch of things just to end it,
a bunch of dashes.
Let's see if that works, very easy this one actually
as it should be, to get the length should not be difficult.
And I forgot a line feed, so let's fix that.
So here it says print individual characters of string reverse or enter the string.
Now scanf, it's only going to read in. So if I enter Jason space Feden.
Scanf is only going to read up to Jason and the space.
You'd want to use another function like f get s if you wanted to read the entire thing.
So what this is going to do it's just going to reverse Jason print out nosaj,
which it does. The characters of string reverse are nosaj.
That's pretty simple, not a very complicated function
or implementation.
Now let's try it with sorting a string.
And so now I'm going to do something. And I'm going to do it in the main I guess just for now.
I should be putting these in actual functions,
but we're not demonstrating functions in this lecture, we're demonstrating strings.
So let's create a couple more local variables that we're going to need.
Mainly, we're going to create a name.
And in this case,
I was going to create an array of strings. We'll create an array of strings, and we'll use f get s.
I'm going to create an array of strings. So this looks like a
two-dimensional array.
So what we're doing here is we're creating 25 strings of size 50.
Then I'm also going to create another one of just 25, this will
contain each string.
And then I'm going to add
two more variables. I'm going to add an n and a j.
And so after we do our reverse, I'm going to print out.
Another message to say what we're doing
sorts the strings
of an array using bubble sort.
I'm not going to explain bubble sort to you. Because again, that's just an algorithm
that is not relevant to what we're doing. Really, it's just a way to sort.
It's one of the easiest sorts actually because you just swap essentially.
But what we want to do is we first want to get how many strings they're going to enter.So we're going to say printf
input number of strings,
and then we're going to say scanf.
Scanf can be used here because it's just a number.
We're going to say percent d, and we're going to store that in our local variable n.
Again, I know I'm not using great names here,
but I'm demonstrating something. Since it's an it's, we have to pass in the ampersand.
Then I'm going to ask them to to enter the strings.
So here I'm going to say
input string
1 2 or 3 followed by a line feed.
This will just be what number they have to enter.
String one, string two or string three.
Then I'll actually have a loop that goes through and gets the input.
So here I'll say for I equals 0,
I is less than equal to n for how many strings they want to enter
and I plus plus or plus plus I.
Here I'm going to actually read the string, and I'm going to use a different way to read -
I was thinking of using f get s,
so that you could enter in a string that had spaces. But I don't think we needed.
We do scan f,
and we're going to read into scanf into our names
array.
Remember, we have name and this is a string of strings essentially
of 25 characters.
The 50 characters 25 words.
So the max number of words you could enter is 25.
So we're going to store this
in our name array. So we're just going to say scanf.
We're going to read in a string, and we're going to put it in
name sub I.
And so we stored our arrays, we can only store up to 25.
Now we're done with our loop and now we can just write our bubble sort.
And so our bubble sort, I'm not going to explain to you, I'm just going to tell you that
it's a pretty simple algorithm that has an inner loop.
So you have a for loop that goes through, I is equal to 1
through all the strings.
And then in your inner loop, you go from zero to
all the strings minus one. And you compare that first string to all the other strings.
And you essentially bubble them down. But what's important here is
the str compare function.
Let me just try to get this indentation correctly
in the str copy functions, this is really what's important.
You'll notice I don't have any ending brackets for either four
because they have single statements.
Even though this if has something in its if body, this is only one statement.
So we're going from we're comparing each word to every other word essentially
after we've compared one previously.
So then we say if str compare name sub j
and name sub j sub one,
so str compare takes two arguments,
two character arrays,
character pointers really.
And if they're equal, it's going to return zero.
If the first parameter
is less than the second parameter,
it's going to return less than zero.
And if the first parameter is greater than
the second parameter, it's going to return greater than zero, one.
So what this is saying is if the first string is greater than the second string,
then we want to swap everything. And that's why we have that temp.
Temp is actually storing a temp temporary string.
So we put in the temp name sub j
and then we swap name sub j with names of j plus one.
That's how the bubble sort works. Again, what's important here is str compare and str copy.
And if you're being safe, you'd want to use strn compare.
And when you do strn compare. Well, actually strn compare, you don't care about
because that's only if you want to compare prefix.
For str copy, you actually want to use strn copy.
And that third parameter is what's important.
So strn copy, you only want to make sure you don't copy in more than the size of temp,
minus one because otherwise you have a buffer overflow.
So you would all these, you could also use --
if it wasn't a character buffer, you probably want to use str length
if it was a pointer. But size of will work fine for this.
And again, this will eliminate any buffer overruns.
So it's always safer to use strn copy
and one because we have our null terminator that needs to go at the end.
So here it's just source destination for strn copy.
We're copying name sub j into temp and the same with names
of j plus one and name sub j. And then lastly, temp into
names sub j plus one. And I'm actually
have to update these variables because they're wrong because they
always have to be the source. So name here name sub j.
And then the last one would be names of j plus one,
j plus one.
So that's strn copy and str compare, very easy to use functions.
After we've sorted them, we just want to print them out.
So we want to say
the strings
appear after sorting,
put it in line feed
and then go through a loop to print out all the strings in our array.
So we say 4 I equals 0.
I is less than or equal to n because the n represents how many
strings we sorted and entered in.
Then we just want to go through our string of string arrays.
So we want to say printf s
line feed and name sub I.
So name sub I is actually our sorted array, that's why we had names name
sub I.
Even though we don't have the second one because that represents the size of each character.
25 words of 50 max.
So let's see if this works.
Build it, got a couple messages because I don't know how to spell.
And if we run this. We're first going to run our reverse function so you can see that again
because we included in the same program.
Say Fedin and idf that's good.
Input the number of strings we want to sort. Let's say five,
let's say zzzz just so it's clear,
something like that.
I messed up a little there, but it looks like it sorted it, a b d f r z.
So it works. I do have one bug though because
input string percent d.
I input the number of strings in n and then I say less than equal,
I should just say less than.
So let's run it again just to demonstrate it, but I have to build it.
Don't forget to build after you've changed things.
Again, we'll just say anything here
and then number strings. Let's just do
four.
So then they alphabetize them and say d e l z, so looks good.
So you can definitely improve on this code where you could want to put everything in functions,
make it more modular. But what I'm trying to get through with this assignment is
how you use str compare, how you use strn copy
and how you can use str length.
You should also play around with the str cat, strn cat
and the that's the other real common function.
You may want to play around with strstr, strchar for searching. Those are really, really good
or the is, upper is, lower for checking characters,
or the atoi nad atof conversion functions.
Thank you. I hope this was helpful.