Course Content

  • string-header
  • time-header

Course Content

Inbuilt Library Functions- String.h Library, Time.h Library

Hello everyone.

In this video we are continuing with the inbuilt function topic.

In the previous video we have seen how with the math.h library we can use different functions.

We also saw it practically, how we use our square root or power function and we could perform operations and functionalities on numbers.

Even in this video we will continue with the same header files and libraries.

So, first of all we will start with our new library, which is called string dot h library.

Like its name, same is its work (jesa iska naam hai wesa hi iska kaam hai) because we know that when we are making strings in our program, then we can operate various functions over the strings like there is a function to find the string length or the function to compare, like this on different strings we can perform the function.

We had already discussed functions related to strings.

We know that when we are handling the string, at that time the string.h header file we have to use because the functions on the top of it, which are already made in the C programming those we could use.

So, here in this topic as well.

We will be able to discuss more functions like our string compare function or the String reverse function, how these functions help us manipulate the strings.

So, what is the string.h library? This is a library which gives access to a lot of functions to play with the strings.

So, when we make one string, we know that string is also one kind of an array.

And according to that the operations can happen over it.

But since this is a set of characters.

So, when we provide different functions over it.

We perform different operations, so the complete sentence that has been made or the complete string that is made, its meaning comes out in different ways.

So, we will be seeing all these things, if we have two different strings, we join them and it becomes one complete string or if we want to do something, that the string is in lower case which means there are no capital letters in it.

So, we have to make all the letters capital.

So, what can we do? All the letters we have to convert are small.

What can be done then? Or I have written the sting reverse.

I want to reverse that, which means I have to get the last letter first.

So, all these functionalities.

If I have to perform these on my string, then I use my string.h header file.

Which means string.h library.

So, we will quickly start and see one by one in this video which all functions we are going to explore.

The 1st function’s name is string compare, we write it as STRCMP.

And its work is, the two strings that we have been given, comparing them and telling us whether these two strings are the same.

One letter of it, is it same to each other or not.

Along with that if we talk about other STRCHR functions.

This function exactly does this so that we pass a string’s character in it.

And this tells us that first time in the string, if we see from the start.

We got this character on which location and it gives a pointer of that location and gives it to us.

So, with this we understand that whichever character we want to put here, exactly the string that is there, where it is actually getting located or where it is placed or is it existing or not.

So, this is the work of our STRCHR function.

So, we will be even using this practically.

But first of all we will see the other functions as well which we are considering under string.h.

So, our third function is the string reverse function.

What does it do? The string that we have provided.

Suppose that we have provided is learnvern string, where it was going to end with N letter.

When we use this string reverse function, it will reverse it and it will read each string and then it will print and give us.

So, this is the work of the string reverse function.

In the same way if I talk about the STRLWR function, it basically works by converting the entire string’s letters in lowercase.

Like we have written the entire string in capital or few parts or letters in capital.

And we want that the entire string should be converted in lower case, at that time this particular function STRLWR, we will be using it over there.

5:12

In the same way is the STRUPR function, this is a string upper function basically and this converts the entire strings letters in upper case, which means it converts in capital letters.

So, these are the functions that we would be discussing here.

Now we will quickly go to our text editor and there we will use these functions and see.

So, here is our text editor.

Frst of all we have imported Our string.h header file here.

Because the functions and functionalities that are written inside it, we have to use here in the program.

That’s why importing and including string,h is very necessary.

Now, we will go into our main function.

And here we have made three strings over here.

The first string’s name is STR1, second one is STR2 and in the same way STR3.

We have made these three strings over here.

And why we have made three here, here we will be using a string compare function.

And what we will do in string compare.

First we will compare STR1 with STR2.

And we will see here whether both the strings are the same or not.

In the same way we will be comparing STR1 and STR3.

With which we will get to know that from both the string which is the same string and which one is not the same string.

6:47

So, here this is the motive and whatever output will come, we will be storing it in the result.

Now in the result which exact value will be stored, that we will be seeing here.

Now we will quickly go to our next line.

And what have we done in the next line? The STRCMP function, which is our string comparing function, that we have used over here.

What can be passed in it? In these brackets we pass two values, the first value is the first string basically.

After the first string comma is put and the second string is passed.

Because of which the string compare function can compare both the strings.

In the same way we have compared STR1 with STR2.

After comparing these two, if both are the same, if both the strings are equal.

Then STRCMP will provide the output as zero and the zero will get stored in our result.

In the same way if we go below, then below STRCMP in the same function, this time STR1 and STR3 these two strings will be compared.

So, you can see that STR1 Learnvern is written.

 But here L and V are in capital letters, in the same way.

If we talk about STR2 string.

Here the entire string is in small cases, in small letters but here STR3 is also exactly the same as STR1.

Even here L and V is capital and the rest are in small cases.

So, here we will execute the program and see whether our STR1 and STR2 are the same or STR1 and STR3 are the same.

So, we will quickly compile it and for compiling we will go on to our terminal.

First, we will clear it with the CLS command.

After that we will run the GCC command over here and we will write our file’s name and we will execute it.

8:58

Over here it has got compiled.

And now we will run it.

So, you can see that the first function that we had run, STRCMP, in which we had passed STR1 STR2, its value has come out to be -1.

It has come as -1 so it means that it is not equal to zero.

And we know that when both the strings are same, they are equal, here the value comes as zero.

So, here it has not come zero, it means that both the strings are not equal.

Why are they not equal? Because we have written Learnvern in both but in the 1st string L and V are capital and in the second one both are small cases.

So, here the comparison function is case sensitive.

That’s why both the strings are not shown equal here.

Here you can see that the STR1 and the STR2’s value is -1.

When the value is -1 or less than 0, that means that in the first string your letters are bigger as compared to what is in STR2 string.

In the same way if you see the other function which was run in which we had provided STR1 and STR3 string, it’s output has come out to be a complete 0.

0 means that here both the strings are the same, they are equal because here L and V are only capital, rest are small and exactly Learnvern is written.

So, this was about comparison and string comparison function.

How it compares both the strings over here.

When both the strings are equal, they are the same, at that time our function gives zero output, otherwise it gives less than zero or more output.

Now this was about the STRCMP function.

Now we will use our STRCHR function.

And how will this function work, we will see over here.

11:05

Before that we will remove the string compare function from here.

We have removed that from here and we will write this code over here in our main function.

One by one we will understand here, all the preprocessor commands are the same.

But in the main function if you see, here what we have done is we have made a string named STR, which we have written as “Welcome to Learnvern”.

And this basically we have made constant by using the CONST keyword.

So, this has become constant.

Along with that you can see that we have made a ch variable and even that is of character type.

And in it we have stored “L” character.

So, if I talk about what we will be doing in this particular program.

So, here we will use the STRCHR function and with that we will get to know the character that we are searching in the string.

That is exactly storing in which particular location or It is located.

And we will get that location with the help of a pointer.

This is the program that we will be seeing here.

So, we will quickly see how it works.

And since we will get it in the form of a pointer.

So, we have to also make a pointer over here, which we have named RET.

And wherever will be the output of STRCHR.

Whatever will be the output of that function.

That we will be storing in our pointer.

So, that we get to know on which location our pointer points.

So, if we talk about STRCHR function, then there are these two things that we have to write in the bracket.

First thing is that we write our string’s name.

In the second part what we write is that particular character, which we have to locate in our string and we have to search.

So, here in CH we have passed L.

And if you see here, what we are doing here is with the help of printf statement, here they will first of all print the character.

The character that we were searching in our string, which is CH, and after that on whichever location our pointer will point.

We will print the string ahead of it, we will not print the earlier string.

Because if we print the string ahead of the pointer’s location, with this we will get to know where exactly the pointer was pointing and where we actually found the character in the string.

 So, that’s why we have printed both the things over here.

 Now we will save it and execute again and see.

Now we will compile it and now we will run here.

So, you will see that our printf statement has been printed here.

14:04

And here the string after L is… which means that we were searching for the L character in it but we have got to know that in “Welcome to Learnvern” we have got L in this particular location.

That’s why with the RET pointer, when we tried to print the further string, it printed Learnvern for us.

Because the pointer had come here and the earlier part did not print here because it was searching the character in this particular location.

And he got it there.

That’s why it was pointing it there and it printed the further string here.

Which we have written as Learnvern.

So, this was about our STRCHR function.

Now we will see the next function which is a string reverse function.

Even this we will quickly implement and see.

What will we do for it? We will remove the rest of the part and we will bring only the printf statement and keep it here.

Now, what is happening in this printf statement, we will see that quickly.

 We already have an STR named string where we have written “Welcome to LearnVern” and here what we are doing is after reversing the string is” then here we will pass a string ahead.

We are not passing the string directly; on the top of the string, we are putting the String reverse function.

You can write it out of the printf statement and store it in some variable and after that you print that variable or you can pass the function directly in the printf statement.

So, we have passed it here directly, our string reverse function.

So, this is our program of reversing the string.

Even this we will compile and execute.

So, here it is giving me a error, we will see the error once, what exactly is the error, the error that is coming here it is telling us that the string that we have made that is one constant that we have made and we don't want that it should stay constant here because we will be basically reversing it with String reverse function.

So, we will remove the CONST keyword that we had written and then again, we will execute our program and see.

So, first we will compile it.

You can see that it has been compiled over here.

Now, we will run it over here.

As soon as we will run it, you can see that after reversing the string, the string has been printed over here NREVNRAEL OT EMOCLEW, which means, if we reverse Welcome to learnvern, if we start reading from the end, then how it will give us the output that is what we have tried to do here with the help of string reverse function, so, this was about our String reverse function.

Now, we will go ahead and see our next function.

What is the next function? Our next function is string uppercase function in which what we will do is, we will try to convert the entire string into uppercase.

We will uncomment this from here and now we will execute it and see.

So, the entire program will remain the same here.

We just have to change our function here.

So, welcome to Learnvern is basically the string here.

Now, we will see how the STRUPR function converts this into uppercase and gives it to us.

So, now here we will go on to our terminal and we will again execute this programme, now you can see that this has been successfully compiled.

Now, as soon as we run it over here.

So, you can see that our few strings were in uppercases and few were in smaller cases.

That entire string has come to us in capital letters.

So, the “welcome to Learnvern” string has been printed with us in the uppercases.

With whose help? With the help of the STRUPR function.

Now, if we do the same one as string lower function So, how will it work, even that we will see.

Here we will replace this with a string upper printf statement, the rest of the things will remain the same.

Now, we will execute it and see, we will go back to the terminal and we will execute it and run it.

So, now you can see that now our entire string has been converted into smaller or lower cases.

So, there W, L and V was capital and now here all the letters have been converted into lower cases.

In this way we can explore different functions related to strings, we can use it in our program, so that our string whichever sentences we make with the help of the string, we can convert them and manipulate them.

Now all these things we can perform on it.

That's why we have learned this string.h header file over here.

Now we will quickly go ahead and we will go on to our presentation where we will be seeing our next header file which means our time.h library.

What exactly is this time.h library? Time.h library is used to access time into the program.

Now in our program, whatever time is on our system, if we print that.

If I want to basically create that scenario, I would use my time.h library.

Now again this time.h library provides a lot of functions related to time.

How you can put two flags in your program, flags means in two such places you write this function, with which what will happen is inside of that whatever program you have written, how much time it will take to execute it.

That will be printed by our function and given it to us.

Along with that, what is the time in our computer, even that it can print and give us? So, this type of functions related to time that can be given to us by time.h library.

It can execute it and give it to us as an output.

So, here we are seeing our time function, local time function, GM time function, these kinds of different functions we will see quickly over here.

So, for this we will go to our text editor and here we will be discussing the time header part, which means we will be discussing time.h header file.

So, first of all to use this functionality we will have to include time.h.

So, we have included it over here, along with that we also have to use the printf function that's why we have imported the STDIO.H header file.

Now, we will come to our main function, in the main function you can see many things that are made related to time.

Now, we will understand them one by one, what exactly they are doing here.

You can see that we have made a struct TM pointer and what exactly is the struct tm.

TM named structure is basically made in a time.header file through which different things related to time such as hours, seconds, minutes, day and all these things, milliseconds, all these things are defined in that structure.

That's why when we are making any program related to time in which we have to use all those time related things, at that time we have to make a pointer for that structure in our program and that structure's name is TM, that's why we are making its pointer.

When we come ahead of it, you can see that we have made one t variable, but its data type is a little different which we have not learned before.

So, this time_t is a different type of data type which helps us in the problems related to time and this is defined inside of our time.h library.

When we have to do things related to time, we use time_t data type and we make one variable of that type.

23:07

Why? Because we know that our integer stores numeric values, along with that float stores decimal values, but when we have to store time, how will the time be? In time we will have hours, seconds, minutes, day, year.

So, the entire date and time part that is there, that basically, complete package we will have to use here and to store that a different type of data type is used and that data type’s name here is time_t.

That's why we are making the same type of variable over here inside which we will store our time and try to get it printed.

So here we have made a time_t type of variable as well.

Now, we will come inside the time related function.

Now what is this time function? This time function basically calculates the time, whenever this particular time function has been called at that particular time which is the exact time in our program basically, it calculates that for us.

So, in the time function we are passing NULL for now.

Because we exactly don't have to pass any arguments here, with which we can say that exactly on this location we want the time or on that location.

We just want the time that is on our computer currently, we have to print it in our program and give.

That's why we are using this time function over here and this time function will give us the time as a return output.

And where will we store that? We will store it in the T variable because its data type is time_t.

That's why we can store the time easily in it.

Now, what we will do is the pointers that we have made, inside the pointer will use this local time function and with the use of local time function we will segregate the entire time, what is minutes, what is hours, what is seconds, we will segregate all the parts of time and we will pass it in PTR.

25:25

Now PTR pointer we had made in struct tm.

And we know what struct tm defines, different parts of the time, which means what is hours, what are seconds at that time, what are the minutes.

So, this pointer in it the local time segregates and stores all the things.

And when we have to print those stored things.

So to print which function we will be using? To print that we will be using the ASC time function, which you can see over here.

It basically converts into string and that string we will be able to print through our printf statement, through printf function.

So, here we have done three things.

First of all, we have made a pointer of structure TM, along with that time_t type of T variables we have made.

And then with time and local time function we have generated time and we have stored it in our PTR with the help of printf function we can print that string and give it.

So, once we will understand it here, how exactly the program will print and give us.

For that we will have to execute it.

For that we will go into our terminal and in the terminal we will first clear it, then we will run our file and see.

So, first of all we will execute it.

So, we have written the file’s name over here.

As soon as we execute it, it has been executed and when it got compiled at that particular time, what was the time that our system has recorded over here.

So, as soon as we will run it and see, I will run it here, you can see that it has printed the time for us over here.

So, this time we can understand over here easily.

Currently the time is 12:39 and 36 seconds.

Along with that year is 2021 and the date is 26th November and the day is Friday.

So, in this way, the time is given to us by all three functions, it segregates the local time separately.

We will separate FRI, we will separate NOV, we will separate 26.

ASC time converts that into string and gives it to us.

So this was about how we can print the particular time of the system in our programme.

Now, I have to do such a thing.

Suppose I've written one program that I'm adding a and b, I am inputting the value of a and b adding it and then getting it printed.

If I want to make this logic in my programme then I know we have to create variables and need to take the user input and need to operate them to print the output as A and B.

I am getting it added and I am getting it printed.

If I want to see how much time did I take to solve this full programme.

so for that here i will use this particular time function.

and we will see how this helps us to find the execution time of the entire programme by using this time function

first i will uncomment this here, so my programme here is uncommented..

here you will see we have created two variables that is start and end.

first we had only made one variable named T, whose datatype was time_t.

buy here we will count two times which is a strat and end.

Let's see why we are counting two?

 suppose the pragrame of taking A and B input and providing it the output.

that is basically till here.

you'll see that this is our logic where we have defined A and B.

we have taken the input of A and B with scanF and printed by adding A and B with print F.

if i want to see that how much time did this logic took then I will remove the time before that, I will store it in the start variable and even after that I will take out time from my computer and I will store that under the end variable.

Now before the start of the program time has come to me in the start variable.

And the program end time has come to me in the end variable.

Now what I will do is the difftime function that we get in time dot H library.

I will use that and I will take out the difference between start and end time.

With that we will get to know how much time we took to complete the program.

Which means, to create a and b, to take its input, to give its output, how much time did we take in total.

This is calculated by our difftime function.

In which we 1st have to enter the last recorded time and the last time that we recorded

that we have to keep second.

 So, this entire time related program is complete.

In which we are taking a and b’s input, with the help of scanf and then we are taking its sum, a+b.

As soon as we do a+b, then we are recording end time over here.

So, we will quickly see how its output comes out.

For that we will execute it again.

And after executing when we will run it, as soon as we run it, it is asking me two values.

I will put here two values 1 and 2.

And as soon as I pressed enter, here “sum of 1 and 2 is equal to 3”.

The total time of taking the input, performing addition on it.

The total time that was required for taking from the input to performing addition on it, the total time that it took, its difference we have got as 9 seconds.

So, “the time taken to print the sum is equal to 9 seconds.” Same thing if I do quickly, I give the input fast.

You can see how the time required will be reduced.

So, I will again execute it over and I will quickly give the input over here.

Here it just took three seconds to calculate it, in the same way if I try to give it quicker.

So, I will give here two different numbers, these numbers would be four and five.

Here it just took one second to take the input of both the numbers and to perform addition over here.

32:27

So, in this way if you want to play with time, you use time in the program, how much time will this take to solve.

All these things if you find out.

So, you can use the time function, local time function and difftime function.

Through which we get a lot of help when we are making any program related to time.

So, in this particular video we have seen only these many functions, we have discussed only these many libraries.

 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.

If you have any queries or comments, click the discussion button below the video and post there. This way, you will be able to connect to fellow learners and discuss the course. Also, Our Team will try to solve your query.

In the coming video we will be seeing running and debugging of the programs, which means when in our programs whenever the error comes.

How we have to solve the error.

We will be basically seeing that in our next video.

Till that time, we can practise all of these functions which we have discussed in this video.

We will meet in the next video till that time.

Thank you so much.

Recommended Courses

Share With Friend

Have a friend to whom you would want to share this course?

Download LearnVern App

App Preview Image
App QR Code Image
Code Scan or Download the app
Google Play Store
Apple App Store
598K+ Downloads
App Download Section Circle 1
4.57 Avg. Ratings
App Download Section Circle 2
15K+ Reviews
App Download Section Circle 3
  • Learn anywhere on the go
  • Get regular updates about your enrolled or new courses
  • Share content with your friends
  • Evaluate your progress through practice tests
  • No internet connection needed
  • Enroll for the webinar and join at the time of the webinar from anywhere