Hello, and welcome back. In
In this lecture, I want to talk about creating a string,
specifically how you can do it in c using a character array.
I tend to use the terms string and character strings interchangeably.
But really a character string is the char array that we're going to talk about.
It's a very important concept.
We're going to be able to now utilize words and text in our program to do all sorts of things.
Now c itself doesn't have any data type that represents a string.
There's nothing there. Many languages
support either an object or a data type of type string.
Now because there is no data type c in string,
you can't use any of the operators on the string.
So what we'll end up doing is we're going to be end up using the standard library
and there's all sorts of string functions that we're going to talk about.
We have three lectures dedicated to the various string functions that you can employ.
And this will help us manipulate the strings, get information about the strings.
But you cannot utilize operators on strings, no plusing
or minusing strings, nothing like that.
Strings in c are stored in an array of type char.
We know what an array is. We know what a type char is.
A type char is a single character, an array
you can just store multiple elements in the same name.
So it makes sense that a string would be inside of a character
array because a string is many many characters.
So characters of a string are stored in adjacent memory cells, one character per cell.
You can access it just like you access an array as well.
But how you would actually declare a string and see is you would simply use the char type
and then the brackets to indicate the size, just like a normal array
if you were to create it of a different type.
Char and then whatever your name is and then the size of the character
array inside square brackets.
That's how you can declare a string in c.
Now when you do declare the string in c like this,
there's no actual data associated with. You're just specifying the size.
That's the simplest way.
So the variable can accommodate a string that contains up to 19 characters.
Remember this null character.
If you say that your string size is going to be 20,
that means only 19 characters can actually be
stored because you have to have that one extra character for the null.
Keep that in mind, declaring a character array is 20
means 19 characters can be stored there.
When you specify the dimension of an array that you intend to use
before a string - to store a string,
it must be at least one greater than the number of characters in the string that you want to store.
The compiler will automatically add the null terminator yo the end of every string constant.
So what you should really do is when you create these character strings,
you should be initializing them.
So you can initialize a string variable just like you can initialize an array.
You put each character inside of these brackets.
So this looks very similar to if you were to initialize elements in an integer array.
But if you say char word and then inside the
squiggly brackets you put in each single character
that would create a character array.
So it's the same as any other array initialization.
In the absence of putting the size,
the c compiler automatically computes the number of elements in the array and then adds one for the null terminator.
So it's based on the number of initializers you have in between the brackets,
curly braces.
This statement is going to reserve space and memory for exactly seven characters,
h-e-l-l-o
exclamation point and then the null terminator, and this little diagram on the right shows you that.
Automatically and adding that null terminator. Now it'd be really
a pain in the butt if you always had to initialize
your character arrays with single quotes followed by commas.
So what you can do is even shorthand notation is you can just put double quotes around that.
So you can either specify the string explicitly, like in that first example,
and then you would have to actually use a string function
just to apply data to it
or you can let the compiler do it for you.
The problem with doing it explicitly is you always have to remember to add that extra character
for the null.
So what you should really do is you should be letting the compiler figure it out for you.
So don't put any numbers inside those brackets for the size.
Because if the size specified is too small, the compiler cannot fit
the terminating null character at the end of the array and it doesn't put one there
and it's not going to tell you that it didn't put one there.
So you're going to have a bug in your program you're not even going to know it.
Best practice is when you're using a string literal
and you're initializing a string to just let the compiler figure out the size for you.
Make it a larger size or something and then just put it in there and it will figure out the size for you.
So do not specify the size. Let the compiler figure it out.
This way you can be sure that it's going to be correct.
Just leave those brackets as empty brackets
and assign it with the assignment operator and then
inside double quotes you can just put it. You don't even need the square brackets.
This is the best practice. We'll go through an example.
You can also initialize just part of an array
of elements of type char with a string.
So if you actually created a size that was really large
and you only assigned a couple characters,
that's fine as well too, you're only initializing some of the characters. All the other characters
are just going to be empty.
So what will happen here is the compiler will initialize the first five elements.
Str zero to str four,
all those characters of string constant and then string sub 5
will contain the null character.
Space is still going to be allocated for all 40 elements in the array
but only five of those are going to be used, four plus the null character.
So again, your best practice don't actually specify a number inside the size,
just assign it a string literal with using double quotes.
That's what you're going to end up seeing.
Now if you didn't initialize and you just created the
character array with a size,how do you put date in it.
Well, because it's array you can't just assign data.
We know that if we had a an integer array,
we can't just assign an integer array the number five,
it doesn't make sense. You have to specify the actual elements.
You have to specify the indices.
So you can't do something like declare a character array with 100 characters
and then say s equals hello. That is not going to work.
Because, again, s is an array. Even though it's a string inside double quotes it's still an array in memory.
So if you're performing an assignment operation you cannot assign
one array of characters to another array of characters like the above.
If you want to assign data to a character array
after you've declared it and after you've initialized it,
you have to use a special function called str copy.
And we're going to talk about that in another lecture.
So up until this point, when you create your strings, they're going to be string literals,
they're going to be inside double quotes, and you're never going to modify them.
So this is perfectly valid as well. You could actually just access these character
or each element in the array by the index and specify each character.
So s sub 0, s sub 1 and then put a single character into single quotes.
You could assign it that way. But imagine if you had a string that was 100 bytes.
This is going to take a lot of lines of code. So you don't want to do that.
What you're going to want to do is initialize it when you declare it
with no size and then specify
if you ever want to modify it you str copy.
As far as displaying a string as output, we're still going to use that printf function.
When you want to refer to a string anywhere in your program,
you just use the array name by itself.
You don't have to specify indices.
Because indices inside the array is going to specify a single character.
If you want the entire string, you just use the array name.
So if you're using the printf function to display output,
you would just do the following. Inside the double quotes,
you would use a percent s as your format specifier and then just the name of the character array.
So you don't treat it like other arrays when you want to access it. When you want to access it just the name no actual
indices or brackets.
And the percent as format specifier is for outputting a null
terminated string. Remember before we used percent d for integers,
you can use percent c for characters.
For strings, you use percent s, and those are character arrays.
It expects some null terminated string.
So however, you initialize the string if you did it with no size
and null terminator is automatically added
or if you created it with a specific size and you added it
or if you added it explicitly, it doesn't matter. It just has to be null terminated.
The printf function assumes one encounters the percent s format specifier
that the corresponding argument is a character string that is terminated by a null character.
That's what it expects, so it has to be that way.
To input a string, we know how to input other
sets of data via the keyboard using scanf.
We're going to do the same thing for inputting strings.
We're going to use a scanf function. With the scanf function,
it's going to be the same way except you're going to use a different format specifier.
So for strings, you're going to use a percent s as input.
And that's used for inputting a string.
The other thing to notice here is there's no ampersand for the variable name
because it's a character. Remember, we mentioned you only have to provide the ampersand
the address of operator on strings.
The reason being is when we talk about pointers it will be more clear.
But char input even though it's a character array it's a pointer,
and scanf as its second argument takes a pointer.
And so that's why you don't have to do it. When you declare integers and doubles, those are not pointers
and. So if you want to
read input from the keyboard using scanf, the second argument expects a pointer
you have to pass in an address.
Now with the scanf when you do a string,
scanf is only going to read up until space.
So if you do scanf and you type in Jason
is a instructor.
The only thing that's going to be stored in input is Jason. Because once it hits that
space, it's not going to read the rest of the string.
So there's other functions that you can use to read the entire string.
Some of the examples we're going to use will probably do that. You can do things like f get s,
which is another way to read input from the keyboard and that will read the entire
string regardless of spaces.
But there's another one I think it's called
get underscore s for C11,
but we'll talk about that more, but that's just one thing to remember
when you're reading input for strings. It's only going to read all the way up until the space.
How about testing if two strings are equal.
Well, up until this point, we have tested
all variables to see if they're equal, we just use equals equals.
But with strings, this is not going to work.
The equality operator, remember, we said that strings are character arrays, so you can't use operators on them.
And the equality operator can only be applied to ints and doubles
or floats or characters.
It does not work on structures or arrays. You can't compare two arrays.
So it's not going to work with strings. If you want to determine if two strings are equal,
you have to explicitly compare the two character strings character by character.
So you'd have to look at each element in the array
index by index.
However, that's really, really a big problem because that would be really tedious.
So we're going to talk about how you can do it very easily
using a str compare function from the standard library,
but you cannot use the equals equals remember that.
And again, just as a reminder, I've said it a couple times I want to say it again,
the string constant x inside double quotes
is not the same as the character constant x inside single quotes.
X inside single quotes is a basic type, it's just char
or care.
X inside double quotes is a drive type, it's an array of type char.
X inside double quotes really consists of two characters, the x
and the null terminator, the null character, so keep that in mind.
Here is an example
of how we can create strings
either declaring them with a size or initializing them on the same line.
Now this is best practice. If you're going to use string literals what I would do is I would do
what it says right here,
create the character array, do not specify a size,
and then to the right of the assignment operator have double quotes.
The compiler will automatically create the right string and add a null terminator to it.
If you specify a size and then you don't have enough characters,it's not going to store it.
So just create character string literals
by using the char data type,
the name of the variable and then empty brackets.
This program is going to I think
count the characters in an array, so it's essentially going to get the length of the string.
What we're doing here is we're creating a variable named count in two character arrays
with two different strings, to be or not to be
and then comma that is the question.
Char str one is going to have an alternator to after b,
and str is going to have an alternative after question.
So we have a loop here that's going to go through the first array,
from count equals zero all the way until we see a null terminator.
And we can use the not equals in this case because we're comparing a character.
Null terminator as a character. It's perfectly fine to compare characters.
So our while loop is going to go all the way until we see a null terminator.
Once we see a null terminator, we're going to jump out.
Count is going to be equal to the number of characters in that string.
So it's the length of that string.
We do it for both strings. And you're going to notice that basically
both strings will have the correct length because we're just going through each one.
To b or not to b is going to be something like
18 characters.
Now you never going to really need to do anything like this because we have a nice
helper function with str length to find lengths of strings.
But again, what this is demonstrating is the fact that it is an array.
And if you want to compare or find the length of a string,
you can use the not equals and look at the character, the null terminator.
I hope this was helpful. This is how we're going to define and initialize string literals.
Thank you.