Hello everyone.
My name is (name) and we are continuing with this C programming course.
In the last video, we had discussed the pointers, we had seen what these pointers are, what are their use in the C programming language and how they can be initialised and declared, all these things we had seen in the last video.
Now is the time to go ahead and now, we will go ahead and straightaway go to the strings, which is our third derived data type.
Now, what are these strings? How do they work? All these things we will quickly see in this video and we will also discuss it.
So, now we will move ahead and search for the answers to this question, what are strings? If we talk about the definition, a string is an array of characters stored in a consecutive memory location.
What is the string saying here in the definition, a string is basically an array but what kind of an array, it is an array whose data type is a character, which means char type, data type would be of any array, we will call that string? This is the basic difference between the single dimensional array and a string.
So, the data type in string would be care and other data types if we will be using, we will call those as single dimensional arrays.
So, this is the basic difference between our arrays and the strings.
After that, we will see, the ending character is always the null character, here the meaning of the null character is one Black slash and zero and it acts as a string terminator.
Now, we will understand it, like in our array, there is one first element and there is one last element.
So, here if we talk specifically about the strings, when our data type is char.
So, here the compiler…as soon as we define or declare the string, our compiler stores this null character in the last element on its own.
Which is defined by backslash n zero and why does it store? It basically says in the coming program, whenever this string will be used, this backslash and zero will tell us that now the string is over.
Why? Because it is always present on the last element.
And as soon as it gets encountered, we know that our string has got over.
So, string basically would be of the character type.
So, these would usually be English sentences or they can also be numerical values.
So, in this way the strings will work.
Now if we talk about its syntax, the string syntax is the same as an array declaration, where there is one data type and after that there is a name, so since here is the string, so its name is string.
And after that there is a bracket in which we are writing its length.
So, this declaration format is the same.
The only difference here is that its data type will always remain char and the other things will be similar to the array.
So, this was about the strings, how strings are different from the arrays and what is stored in the last element.
So, all these things we have seen in this particular slide and we have learned.
Here there is one more clear picture that has been shown in which you can see that one array is made and inside that H E L L O, these 5 characters are stored, which means it is trying to say hello and after that at the last element we will see \0 has been stored by the compiler automatically.
And this we call a null character.
Whenever this array, this is a string basically, if with this string we encounter \0, it will mean that our string has got over here.
So, the compiler automatically places the null character at the end of the string, when we initialise the array.
When we tell the size of the array, when we initialise it, along with that, on its own this compiler puts this null character, if its data type is char.
Which means, if it is string, it can do so.
So, this was about a null character, about strings.
Now, we will move ahead and see the strings or character type arrays which other functions we can put on it, which other functions we can use.
Along with a C programming language, which are the functions that come by default, there are many such functions which we can use along with the strings.
But here we will see 4 such functions one by one, which are used very commonly.
And their use is very effective in strings.
So, let's start with our 1st function.
And this function is strcpy.
This basically means string, copy and what does it work? It copies and pastes all the things from one string to the other.
We made one string in which we have stored Hello and the other stick is empty.
So, what will this strcpy do? It will copy all the things from the full string and it will paste it in the empty string.
If you have to duplicate one string or we have to make a copy, at that time we use this function.
This function comes with our C programming.
So, we don't have to write a function definition separately for it.
If you go ahead, we will talk about the other function which is called strlen and this means that string length.
What work does it do? It returns the length of the string not counting the null character.
So, this basically ignores the null character and it calculates the length of the real string and tells us.
Like there is Hello in our string.
So, in Hello it will tell us that our string’s length is 5.
So, in this way our strlen function works, if we go ahead, so ahead you can see, the third function that we are seeing, we are calling that strcmp and this means string compare.
Now how does this work.
If there are any two strings, if these two strings are exactly the same even case wise.
At that time what does it return, this function returns 0.
If inside it 0 returns, with this we understand that the two strings that we had passed in this are ditto, the same.
So, this was about the strcmp function, which is the string compare function.
Now, if we talk about the fourth function, the fourth function is strcat, which is string concatenate, what does it mean? It basically appends one string to another, creating a single string out of the two.
If we have two such strings, which we have to join one after the another, and make it complete one string, that we can do easily with this particular function.
We just have to give the names of both the strings and it will join it and give it and give us.
So, we have seen and understood all the functions over here.
And we have also seen all its uses.
Now is the time to practically explore the strings, which means exploring things in our text editor, visual studio code.
So, we will quickly go on to our Visual Studio code and we will see one by one what this program is trying to do from us? This program is about the strings and we will make a few strings over here and initialise them and we will also use some functions over it, which we have discussed right now.
So, quickly we will start and see our first line which is preprocessor command one.
And in the preprocessor command, here you can see these two lines.
Now which are these two lines.
The first line is stdio.h one, which is basically used for input and output.
But you can see the other preprocessor command.
What is it doing? #includestring.h.
ash include string dot h so here there is a point to concentrate on the string dot h file get gets very important to be imported, if we’re making the strings and using the strings function in our program, during the time we don't import this file with the help of preprocessor command in our programme, till that time our strings will not work.
It will not work properly.
That's why this string.h file is very important to import.
In the last videos we had discussed these files come with the built-in functions.
So, because of the functions that we are going to use in the program, we don't have to write their function definitions.
Why? Because it is already defined in string,h file.
So, this was about our preprocessor commands.
Now we will move ahead and talk about our inside the main function.
So, inside the main function, you can see that here we have made three strings.
So, in the first string we have stored H E L L O, this string.
And you can see that its data type is char.
For all three strings it is char.
The way of declaring it is completely similar to arrays.
But since it is a char type data type array.
So, its flow of declaring, the brackets, those are different.
And here you use the double quotes instead of brackets.
So, you can see that in the first string, which is the s1 string, we have inserted Hello and the second string is s2, in which we have inserted World.
So, we have made these two strings, s1 and s2.
Hello and World, respectively we have got them stored.
Now, if we talk about the third string, this is the s3 string and we have given its size as 20.
But in it we have not done any initialization.
Which means inside it we have not stored any value.
So, how will it work? Even this we will understand in this program.
So, we will move ahead.
We have defined a variable named len, it is of integer type.
So, these things are clear, how we have made these strings and after that we have also made a variable with the name of len.
Now, we will come to our first function, which is our string copy function.
So, what does the string copy function do? The string copy function in s3 and in s1, it will complete the copy and paste function.
So, we will see here the parameters that we have passed in string copy function, s3 and s1.
So, the content of these two will copy and paste.
here basically the content of these two will be copy and paste over here.
So, we will see ahead, how it is copy and pasting and what is its output.
So, as soon as we will run its code, we will get to know about it.
Why? Because we have made a printf statement along with it.
Which will print the exact thing that is s3 for us.
After copying in s3 the data that is coming, how will that be displayed, that will be told to us by this printf statement.
If we talk about other functions like string concatenate function.
So, even here we have passed two strings in our function’s parameter which is s1 and s2.
Now what will s1 and s2 do to this function? It will join it.
So, it will start with s1 and it will 1st print the s1 data and it will join s2 as well over there.
So, whenever we want to print s1, basically what it will do? It will print s1 and s2 and show it here.
So, what does this string concatenate function do? It will add s1 and s2 function and it will store again in s1.
So, here we will see, how does its working happen.
How does string concatenate works? If we talk about the third function, the third function is of string length and in this we have given only one parameter because with only one parameter we will make out what is the length of our string, this string length will give out particular length output.
Where we will store that? We will store it in the len variable which we have already made on the top.
Now in len, our s1 string length has come.
So, now we will simply get them printed through printf statement.
In s1, Hello is stored.
In s2, world is stored and in s3 for the start nothing has been stored for now.
But as soon as we will run this function, s3 will also get complete.
So, we will save this programme.
And now we will right click and run code and we will see what is the output that we will see here.
So, you can see here that we have run all three functions over here and the first function which was there, which was of string copy, where we have added s3 and s1.
So, you can see that since s3 was vacant and in s1, there was data.
So, when we copy pasted, s3 and s1, what happened? The data gets stored in s3 and when we got s3 printed, you can see over here that s1’s Hello has been printed over here.
So, this was about string copy function.
So, if we talk about string concatenate function, you can see that we had considered both s1 and s2, in this function.
We will see that the strings that were stored in s1, which means Hello that was stored that got printed here 1st.
And after that in s2, even word was stored.
So, that world got stored after Hello.
Which means, Hello and World met each other and they joined and became as one string.
In this way our string concatenate function works, we will see once again towards our third function which is string length function.
And in that we put only one parameter which is s1.
So, we will see the length of the s1 string.
So, if you will run this function here, and the variable len, if we get it printed over here.
You can see that its size, which means 10, even that is getting printed over here.
So, s1’s size is getting printed over here, which is 10, In this way, all three functions work when we use strings.
But keeping this thing in mind is very important.
When we are using these functions or when we are making these strings.
It becomes important that you use this sting.
h one preprocessor command and write in your file.
As if this file is not there then none of the string’s functionality, which we have discussed in this video and saw how strings are different from arrays.
If you think that there is any part of this video or this topic, you might have not understood then you can ask us any query or question related to that topic without any inhibition.
You just have to go on forums.learnvern.com, you have to type your query there in your own language and we will come up with the solution or the answer as soon as possible in your own language.
Apart from this if you wish to have a discussion on any topic.
Even that you will be able to do on forums.learnvern.com.
In the next video we would be discussing structures used in C programming.
Structures are also one type of derived data types; how do they work? What will be there in it and what will not? All these things we will discuss in the coming videos.
Thank you so much.
Share a personalized message with your friends.