Hello and welcome everyone to this practical module…..
Like we were seeing models one by one different practical in this module.
In the same way we have got a new problem statement, which is called reversing a string.
Now here you will say that even before we have reversed a string and seen when we had discussed string.h library.
There with the use of STRRVE function, we had reversed our string very easily.
So, this is a very efficient function which we have, but if I want that if I want a user defined function for my own self, for this work, for reversing a string.
In that situation, how will I be making it? That is what we will discuss here in this problem.
Earlier we used to only talk about a simple STRRVE function but in this video, in this topic we will be discussing one user defined function, how we will make its logic and how will we use it, will we write the parameters in it or not? All these things we will be discussing in this video.
So, without any delay, we will start reversing a string topic and we will quickly go on to our text editor and we will see what is in store for us over there.
So, now we will go on to our text editor and here first of all the line that we will be writing for our program would be obviously and definitely be our preprocessor command, which is #include stdio.h command.
After writing this command.
We will be making one user defined function, we will be making our own function for which we all know that we will be needing function definition, till now we were starting our main function after the preprocessing command.
In this case we will be writing the function definition of the user defined function first and after that definition, we will be starting our main function.
So, what will be written inside the definition, in function definition, function’s logic will be written.
How will we reverse that string? So, for that we will be making one function first.
The function’s name we have kept as a reverse string and its return type is void.
And inside this we will not be considering any parameter.
Since we are not considering the parameter, you will ask me how we will take that string? How will we take the string from the user? So now we will understand it here in some time.
So, we have made one function for ourselves which is called reverse string.
And now we will directly write the function definition.
There is no need of function declaration over here.
Now we will write our definition.
So, inside the definition the first work that we will be doing is of taking one character input work, which we are taking from the C variable named variable and here with the help of scanf function, basically we are taking that character inside it.
Now you will say that we were going to reverse the string.
So why we are taking the character over here, why we are not taking the string directly.
We will talk about it now.
If I say that I want to make a function to reverse the string, in that case I have taken the decision to make a recursive function over here.
Now we all know that what is recursive function, if we call the function inside the function definition then the function repeatedly starts running the statement, which means that the work happens in this way that once the function is called in the function definition, in that function call again that definition will be executed.
And inside that one more function call we will get and then we will complete that function call and then again inside it one function call we will get, in this way, we will keep on going inside till the time the condition doesn't turn out to be false in the statement.
So, as soon as it is not satisfied or it becomes false.
We will start coming out slowly, slowly from our each function call and then our entire function definition will start getting executed.
So, this is the concept of our recursive function.
So, in the same way what we will do here is in our function definition, we will call the same function, as and when the function will happen, we will take input of each and every character.
We will keep on taking input of each and every character and we will keep on deep diving in our function, with the help of function calls.
And when our last character will be input, when our last character will be input at that time the condition will be changed over here.
How will that be changed? We will see that over here.
But as soon as our last character will be read the condition will be changed and then we will print the last character over here and then we will come out of that function call.
And we will get to know that we also have a second last character over here and then it will be printed.
Then we will come out of it then we'll get to know that there is a third last character as well.
In this way we were taking the characters one by one from the starting but when we are printing them, we will be printing them from behind, because we kept on going into the function calls of our function definition.
We kept on going, we kept on doing and what happened because of it.
Where did we stop? We stopped on the character of our last string.
Now from that character we have now started printing.
So, as and when we started going back we started printing that string in reverse manner.
To see that thing completely, we have taken the input of the character.
Now we will put one condition over here and what will be the condition, the condition will be the character that has come in C, is it \n character?. Which means is it the newline character or not? So, in this condition, we will be checking that in case the user has passed one string and what has happened in the string, there were spaces, numbers and characters as well.
But when those string ends, we know that after that a new line character comes.
If the new line character is coming, that means that our string is now over.
We don't have to take any input from the user any more.
And that's why the condition has been checked over here.
Till the time c is not equal to \n, till the time we don't reach the end of the string, till that time which statement we will be running.
Till that time we will be calling this same function again.
And after calling it again for that particular scenario or situation this entire function definition will run again and this will keep on going, on and on.
As soon as we will get out of it.
After that, we will be printing it with the help of this printf function.
So, here we will understand it once again.
Suppose we have inserted one character, since in the scanf they are inserting the character, as it is in the input we will give the entire sentence but this program will consider each and every character.
Because we have called it format specifier that you have to consider the character.
So, first it has considered the character suppose we want to pass learnvern like a string.
So, the first character that comes in our function is L and L has been stored inside C and this condition was checked, the character that is stored in C is it this?, L is not equal to this.
So, now we will come inside it, inside if condition and again we will be calling the reverse string function.
Again, when it will be called we will go again over here and again after coming here to the starting position.
Again, we'll use this next character input over here,, the next character here would be E because Learvern is LEARNVERN.
So, in this way the next character will be E and E will also be checked whether it is equal to the newline character or not.
If it's not, we will again call the reverse string and it will consider one more next character, in this way LEARNVERN, these characters will be considered by us and there will be one condition, what will happen in that?. Then there will be no need to call reverse function and when this condition will be touched after that, we will print that character with the help of printf function.
We have traversed this entire string, Learnvern, we have taken l, then e, then a, then r, then n, then v, then e, then r, then n.
As soon as n came, after that we printed n.
And after that we went outside and whenever we were deep diving inside the function, when we started coming out of the layer of the function one by one and at that time we had read our character, that character is getting printed over here.
So, the first n will be printed, then r will be printed, then e will be printed, then v will be printed.
In this way all the characters will be printed in a reverse manner.
So, this function is called as the recursive function, this entire concept is called as the recursive concept.
We have called one function inside it itself and after calling it inside, again that function definition is used and again it was called and again the function definition was used, in this way we kept on using it.
So, this is our function which we have made here now.
After that we have to again call it in the main function.
Right? So, for that call we will be writing our main function and what we have written in the main function? You can see, we have told the user first that you enter the string.
What will the user say? Okay, we will enter it and when they're entering, at that time we have called this reverse string function and as soon as it gets called.
Here through scanf it will take input one by one.
And after taking the first input, again there will be a function call and the second input will be taken and then again function call will happen and third input will be taken, in this way by taking input we will reach this condition that our string has come to an end and at that time we are going to do printf.
And we will keep on printing.
Whom? Each and every character, we are printing every character from behind.
Because we have come right inside the function.
How are the function? The outside function, uses the first character, second, the function inside it, it uses the second character.
In this way the inside functions that are there, they consider last characters respectively.
So, that's why whenever we will call this reverse string function, one by one the recursive functionality will work and the input will be taken and the same input with the help of reverse string function, we will be printing in the reverse order.
So now to see that how it is executed we will run it and you will see that for running it we will write the command over here.
First of all we will open our command prompt and here we will write the command, “gcc reverse string.c”.
As soon as we write this command you will see that it has got compiled, now we will run it.
After running, it is asking us that you tell us your sentence which you want to reverse, “this is learnvern”.
So, in this way, we will write one sentence, this is learnvern is the sentence that we have written.
Our desired output will be in such way, it will read from the right-hand side and print it.
So, you can see that it has reversed it and given us our string.
Now it has printed NREVNRAELSISIHT.
So in this way our reverse functionality of a string works, in this way it generates the output for us.
And whenever we want to use the recursive function, at that time, we design this kind of logic.
To reverse the string there are many such ways in which you can execute and see, implement.
Till that time you keep on practising all these problems and questions, all these logics.
And if this topic related there is any part which you have not understood or there is any part of this program which you are finding doubtful.
You can definitely go on to forums.learnvern.com.
And there you can post your query by selecting your course and related to that cause, we will give you answers in your own language at the earliest.
So, you keep on practising.
We will be meeting you in the next video till that time, thank you so much.
Share a personalized message with your friends.