Hi, everyone.
So in this video, we are going to solve this question, convert a string to integer, so I have a string,
one, two, three, four, five.
So this is string and I want to convert this string into indigenous or integer will be one, two, three,
four and five.
So twelve thousand three hundred and forty five.
OK, so given a string I want to convert it into an integer.
So how are we going to solve this question so we can easily solve this question with the help of all
over by loop.
But we will use recursion to solve this question.
So let's see how it and will help us to solve this question.
So it's very simple.
You have this one, two, three, four and five.
So this is basically your string.
So what will do?
I will break our problem like this, so you have this this is you ready you will call the guys in on
this area.
So if you will call, let it go.
This area.
So so the output will be an integer.
So this area will be converted into an integer.
So after this area.
So the string one, two, three, four.
So string one, two, three, four converted into an integer.
So this will be one, two, three and four.
So this is the answer returned by that equation.
So the equation will convert this string in 20 years.
So I will get the integer and let's call it, Smolan said.
So what I will do last digit is basically five, so I will convert character five in twenty five, I
will simply write like this character five minus character zero.
So the output will be integer five because we know character minus character is integer.
So I will go to five.
So I know the small answer, which is one, two, three, four.
I know the last digit.
So this is the last digit.
Now, what I need to do, so I will multiply Smolan Surbiton and I will add last digit, so a small
answer is one, two, three, four, one, two, three, four, multiplied by ten plus last digit.
So this will be one, two, three, four and five, which is the right answer.
OK, which is detailed answer.
So approach is very simple.
You will call that it in on this area.
So this idea is basically.
So this is, let's say, string and a number of elements and minus one.
So this will give us.
So the question will convert this area because it will convert this part of the string to an integer,
so I will get, Smolan said.
I will get someone to answer, I will multiply small answer by 10 and I will add last digit.
So last digit is currently a character and I need to find out the integer.
So I will, but I will also.
The last resort is basically string and minus one.
So this is this Nix's and minus one.
So string and minus one will give me the character.
I will minus, I will subtract zero and I will get the last digit.
So approach is very simple.
One, two, three, four and five.
What we will do so first of all, you will call the on this area, so it will be converted to one,
two, three, four.
So basically string one, two, three, four is converted into this.
This is smaller and smaller.
So multiply by 10 plus last digit will be one, two, three, four and five.
And this will be our final answer and we will have done this simple.
So in order to pass this small area, in order to pass this area, what are you to do?
You need to take you will pass and minus a number of elements and minus one.
So N means basically the land.
So we need a land function.
So we have already written the code for the land function.
So let's create a string.
So my string is basically, let's say one, two, three, four, five.
One, two, three, four, five.
This is my string.
I want to convert it into an integer.
So let's write the function.
The name of the function will be.
Convert string and there will be integer, so convert string.
To India, what it will take, so it will take the character very.
It will take the number of elements and and what is known and is basically the Lent, so it is will
call the land function number of elements.
And I will give string area, I will give my strength, so the base case is very simple.
If a number of elements are zero, if the size is zero but Reverand deteriorate, there will be zero.
So if thing is empty.
I will return zero if this thing is complete, then if we convert the Palestinians, you will get zero.
Otherwise what are you to do?
So you need to call dedication.
This approach is very simple.
This is one, two, three, four and five, what I will do, I will call variegation on one, two,
three and four.
So called verification on this small string, so basically a number of elements and minus one, and
it will give the integer one, two, three and four.
So this is our small answer.
So it is a very small answer, so integer.
Smart answer is basically, I will call the function convert string to integer.
String theory and number of elements and minus one.
So after getting this more answer, after getting one, two, three, four, I need to find out the
last digit.
So currently the last -- is a character, so string and minus one, the index is in minus one.
So this is basically a character I want integer.
So I will subtract the zero and I will get the integer.
So let's try to find out the last digit.
So what is last digit.
So this is.
The last element is a character minus zero, character zero, so you will get the Integer and Virgilia
final answer.
So your final answer will be you will multiply this small answer.
So one, two, three, four will be multiplied by 10 so that it will become one, two, three, four
zero.
And then you will add the last digit.
Last digit is five.
OK, so multiply by 10 and then add the last digit and this will be your final answer.
So what is our final answer?
So our final answer is basically our small answer, we will multiply a small answer by 10, we will
add last digit and that is our final answer.
So we will return our final answer.
So I think our function will work.
So let's start functioning.
So let's call the function convert to string.
Let's give it string.
And this function well.
Diana DeGette, 23, is basically convert toasting and now we will print a so is an integer.
So A's integer.
One, two, three, four, five.
Simple.
So let's check.
OK, so this is not working, so I need to pass two things, I have to pass a number of elements also.
So a number of elements is in.
And this is our A.J., one, two, three, four, five.
So basically.
So this is our integer one, two, three, four and five.
Simple, so that's how it is working, so let's write in this code so that you guys can understand.
So let's write in our code for this example.
Only one, two, three, four and five.
So I have one, two, three, four, five.
So I will call you guys in on one, two, three, four.
Then I will call that equation one, two, three, then I will call Erica Jong un went to.
Then I call that equation on one.
So a number of elements went ahead and then I recall that equation ampitheater.
So basically a number of elements.
Zero seven zero zero.
So I will return zero to a small answer is basically zero.
You will find out the last digit last digit is one.
So zero multiplied by ten plus one.
So I will return one.
So Lastminute is basically two Los Angeles two, so one multiplier 10 plus two, so I will return to
El Lastminute this basically so 20 multiplied 10 plus three, so I will return 123.
So last digit is basically four.
So small answer is 123.
Smolan is 123 so small and multiply digit so multiply 10.
LaserJet is four.
So I will return one, two, three, four.
So small answer is one, two, three, four, lastminute is five, so multiply by ten and plus five.
So you've done one, two, three, four and five and that is the correct answer.
So that's how our function is working.
So right now, our function will not work for cases like minus 123.
So this is a valid integer, right?
So if there's a string, if this thing is minus 123, then your output should be minus 123.
But our code will not handle this.
Similarly, if nothing is plus 123, so the digital part will be 123.
So currently our code will not handle this.
Similarly, if the input string is wrong, for example, the inputs doing this, one, two, maybe three,
four.
So if this is the string, then you cannot convert the string into the integer.
OK, so this string cannot be converted into an integer and our function will not handle this case also.
So our function has a lot of limitation.
It will work only for specific inputs.
OK, so for example, string C, you are putting string so string can be very, very long.
So if this is our string, one, two, three, four, five, six, seven, eight, nine, one, two,
three, four, five, six, seven, eight, nine and so on.
So string can contain as many number of characters as you'd want.
So string can be very, very large.
So can you convert this string into an integer?
No.
So Integer has basically a very small range.
OK, you are returning integer so you cannot convert this big number into an integer.
OK, so this type of string, a very long string, cannot be converted into an integer.
So I think an integer you can store around nine digits, we can store around nine digits and not more
than nine digits.
If you want to store it in digits, you need to use that the type longlong, but our string can contain
as many number of characters as it wants.
So if this thing is containing 20 letters containing 20 characters or let's say two hundred characters,
then this cannot be converted into an integer so that a function will not work for those cases also.
So our function will work only for small inputs and for right inputs.
OK, so this is from this video.
I will tell you the next one.