1 00:00:01,940 --> 00:00:02,700 Hi, everyone. 2 00:00:02,719 --> 00:00:08,810 So in this video, we are going to solve this question, convert a string to integer, so I have a string, 3 00:00:09,170 --> 00:00:10,360 one, two, three, four, five. 4 00:00:10,700 --> 00:00:16,309 So this is string and I want to convert this string into indigenous or integer will be one, two, three, 5 00:00:16,309 --> 00:00:16,930 four and five. 6 00:00:17,480 --> 00:00:19,640 So twelve thousand three hundred and forty five. 7 00:00:19,670 --> 00:00:23,480 OK, so given a string I want to convert it into an integer. 8 00:00:23,960 --> 00:00:27,740 So how are we going to solve this question so we can easily solve this question with the help of all 9 00:00:27,740 --> 00:00:28,450 over by loop. 10 00:00:28,700 --> 00:00:31,160 But we will use recursion to solve this question. 11 00:00:31,550 --> 00:00:34,490 So let's see how it and will help us to solve this question. 12 00:00:35,260 --> 00:00:36,280 So it's very simple. 13 00:00:36,290 --> 00:00:38,570 You have this one, two, three, four and five. 14 00:00:39,410 --> 00:00:41,210 So this is basically your string. 15 00:00:43,100 --> 00:00:43,970 So what will do? 16 00:00:44,870 --> 00:00:51,080 I will break our problem like this, so you have this this is you ready you will call the guys in on 17 00:00:51,080 --> 00:00:51,520 this area. 18 00:00:53,120 --> 00:00:54,390 So if you will call, let it go. 19 00:00:54,680 --> 00:00:55,020 This area. 20 00:00:55,060 --> 00:00:56,920 So so the output will be an integer. 21 00:00:57,260 --> 00:00:59,630 So this area will be converted into an integer. 22 00:01:00,470 --> 00:01:01,880 So after this area. 23 00:01:01,880 --> 00:01:03,550 So the string one, two, three, four. 24 00:01:03,860 --> 00:01:06,490 So string one, two, three, four converted into an integer. 25 00:01:06,500 --> 00:01:08,460 So this will be one, two, three and four. 26 00:01:08,990 --> 00:01:11,450 So this is the answer returned by that equation. 27 00:01:11,880 --> 00:01:14,420 So the equation will convert this string in 20 years. 28 00:01:14,480 --> 00:01:17,690 So I will get the integer and let's call it, Smolan said. 29 00:01:20,160 --> 00:01:27,570 So what I will do last digit is basically five, so I will convert character five in twenty five, I 30 00:01:27,570 --> 00:01:31,390 will simply write like this character five minus character zero. 31 00:01:31,410 --> 00:01:36,620 So the output will be integer five because we know character minus character is integer. 32 00:01:38,010 --> 00:01:39,420 So I will go to five. 33 00:01:39,700 --> 00:01:42,180 So I know the small answer, which is one, two, three, four. 34 00:01:42,180 --> 00:01:43,320 I know the last digit. 35 00:01:43,320 --> 00:01:44,730 So this is the last digit. 36 00:01:46,560 --> 00:01:53,610 Now, what I need to do, so I will multiply Smolan Surbiton and I will add last digit, so a small 37 00:01:53,610 --> 00:01:58,140 answer is one, two, three, four, one, two, three, four, multiplied by ten plus last digit. 38 00:01:58,320 --> 00:02:02,100 So this will be one, two, three, four and five, which is the right answer. 39 00:02:02,130 --> 00:02:03,850 OK, which is detailed answer. 40 00:02:05,250 --> 00:02:06,500 So approach is very simple. 41 00:02:08,699 --> 00:02:10,479 You will call that it in on this area. 42 00:02:11,370 --> 00:02:12,820 So this idea is basically. 43 00:02:12,840 --> 00:02:17,770 So this is, let's say, string and a number of elements and minus one. 44 00:02:18,090 --> 00:02:19,740 So this will give us. 45 00:02:20,790 --> 00:02:25,910 So the question will convert this area because it will convert this part of the string to an integer, 46 00:02:26,220 --> 00:02:28,110 so I will get, Smolan said. 47 00:02:29,430 --> 00:02:34,980 I will get someone to answer, I will multiply small answer by 10 and I will add last digit. 48 00:02:36,570 --> 00:02:41,130 So last digit is currently a character and I need to find out the integer. 49 00:02:41,140 --> 00:02:42,970 So I will, but I will also. 50 00:02:42,990 --> 00:02:46,210 The last resort is basically string and minus one. 51 00:02:47,160 --> 00:02:49,230 So this is this Nix's and minus one. 52 00:02:49,500 --> 00:02:51,660 So string and minus one will give me the character. 53 00:02:51,660 --> 00:02:55,680 I will minus, I will subtract zero and I will get the last digit. 54 00:02:56,310 --> 00:02:57,980 So approach is very simple. 55 00:02:59,220 --> 00:03:00,690 One, two, three, four and five. 56 00:03:01,800 --> 00:03:07,920 What we will do so first of all, you will call the on this area, so it will be converted to one, 57 00:03:07,920 --> 00:03:08,520 two, three, four. 58 00:03:08,910 --> 00:03:12,590 So basically string one, two, three, four is converted into this. 59 00:03:13,030 --> 00:03:14,360 This is smaller and smaller. 60 00:03:14,410 --> 00:03:19,640 So multiply by 10 plus last digit will be one, two, three, four and five. 61 00:03:19,650 --> 00:03:23,020 And this will be our final answer and we will have done this simple. 62 00:03:24,210 --> 00:03:28,470 So in order to pass this small area, in order to pass this area, what are you to do? 63 00:03:28,470 --> 00:03:32,910 You need to take you will pass and minus a number of elements and minus one. 64 00:03:32,910 --> 00:03:34,310 So N means basically the land. 65 00:03:34,710 --> 00:03:36,090 So we need a land function. 66 00:03:36,840 --> 00:03:39,120 So we have already written the code for the land function. 67 00:03:39,420 --> 00:03:42,300 So let's create a string. 68 00:03:43,170 --> 00:03:46,170 So my string is basically, let's say one, two, three, four, five. 69 00:03:47,040 --> 00:03:48,220 One, two, three, four, five. 70 00:03:48,240 --> 00:03:49,020 This is my string. 71 00:03:49,020 --> 00:03:50,730 I want to convert it into an integer. 72 00:03:51,300 --> 00:03:53,070 So let's write the function. 73 00:03:53,370 --> 00:03:54,750 The name of the function will be. 74 00:03:56,670 --> 00:04:02,880 Convert string and there will be integer, so convert string. 75 00:04:05,280 --> 00:04:10,200 To India, what it will take, so it will take the character very. 76 00:04:12,630 --> 00:04:20,190 It will take the number of elements and and what is known and is basically the Lent, so it is will 77 00:04:20,220 --> 00:04:22,530 call the land function number of elements. 78 00:04:23,520 --> 00:04:30,000 And I will give string area, I will give my strength, so the base case is very simple. 79 00:04:30,010 --> 00:04:35,520 If a number of elements are zero, if the size is zero but Reverand deteriorate, there will be zero. 80 00:04:36,610 --> 00:04:37,870 So if thing is empty. 81 00:04:38,900 --> 00:04:44,810 I will return zero if this thing is complete, then if we convert the Palestinians, you will get zero. 82 00:04:45,270 --> 00:04:46,600 Otherwise what are you to do? 83 00:04:46,970 --> 00:04:48,460 So you need to call dedication. 84 00:04:49,160 --> 00:04:50,390 This approach is very simple. 85 00:04:53,460 --> 00:04:59,910 This is one, two, three, four and five, what I will do, I will call variegation on one, two, 86 00:04:59,910 --> 00:05:00,540 three and four. 87 00:05:02,410 --> 00:05:07,330 So called verification on this small string, so basically a number of elements and minus one, and 88 00:05:07,330 --> 00:05:09,940 it will give the integer one, two, three and four. 89 00:05:10,180 --> 00:05:11,820 So this is our small answer. 90 00:05:14,000 --> 00:05:17,780 So it is a very small answer, so integer. 91 00:05:19,380 --> 00:05:24,600 Smart answer is basically, I will call the function convert string to integer. 92 00:05:27,260 --> 00:05:30,140 String theory and number of elements and minus one. 93 00:05:32,120 --> 00:05:36,800 So after getting this more answer, after getting one, two, three, four, I need to find out the 94 00:05:36,800 --> 00:05:37,430 last digit. 95 00:05:37,670 --> 00:05:45,060 So currently the last -- is a character, so string and minus one, the index is in minus one. 96 00:05:45,620 --> 00:05:47,990 So this is basically a character I want integer. 97 00:05:48,020 --> 00:05:51,170 So I will subtract the zero and I will get the integer. 98 00:05:51,590 --> 00:05:53,420 So let's try to find out the last digit. 99 00:05:53,990 --> 00:05:56,540 So what is last digit. 100 00:05:56,870 --> 00:05:57,650 So this is. 101 00:05:59,460 --> 00:06:08,280 The last element is a character minus zero, character zero, so you will get the Integer and Virgilia 102 00:06:08,280 --> 00:06:09,040 final answer. 103 00:06:09,570 --> 00:06:13,300 So your final answer will be you will multiply this small answer. 104 00:06:13,320 --> 00:06:18,540 So one, two, three, four will be multiplied by 10 so that it will become one, two, three, four 105 00:06:18,540 --> 00:06:18,870 zero. 106 00:06:19,080 --> 00:06:21,150 And then you will add the last digit. 107 00:06:21,600 --> 00:06:22,870 Last digit is five. 108 00:06:22,900 --> 00:06:27,600 OK, so multiply by 10 and then add the last digit and this will be your final answer. 109 00:06:28,800 --> 00:06:30,350 So what is our final answer? 110 00:06:33,860 --> 00:06:39,890 So our final answer is basically our small answer, we will multiply a small answer by 10, we will 111 00:06:39,890 --> 00:06:43,510 add last digit and that is our final answer. 112 00:06:43,520 --> 00:06:45,050 So we will return our final answer. 113 00:06:45,950 --> 00:06:47,570 So I think our function will work. 114 00:06:48,170 --> 00:06:49,580 So let's start functioning. 115 00:06:49,580 --> 00:06:52,590 So let's call the function convert to string. 116 00:06:52,610 --> 00:06:53,930 Let's give it string. 117 00:06:54,380 --> 00:06:55,520 And this function well. 118 00:06:55,520 --> 00:07:03,830 Diana DeGette, 23, is basically convert toasting and now we will print a so is an integer. 119 00:07:04,820 --> 00:07:06,140 So A's integer. 120 00:07:06,140 --> 00:07:07,130 One, two, three, four, five. 121 00:07:07,640 --> 00:07:08,150 Simple. 122 00:07:08,840 --> 00:07:10,130 So let's check. 123 00:07:14,660 --> 00:07:19,840 OK, so this is not working, so I need to pass two things, I have to pass a number of elements also. 124 00:07:20,210 --> 00:07:21,590 So a number of elements is in. 125 00:07:26,450 --> 00:07:29,120 And this is our A.J., one, two, three, four, five. 126 00:07:32,220 --> 00:07:33,000 So basically. 127 00:07:34,760 --> 00:07:37,130 So this is our integer one, two, three, four and five. 128 00:07:38,230 --> 00:07:43,100 Simple, so that's how it is working, so let's write in this code so that you guys can understand. 129 00:07:43,930 --> 00:07:46,100 So let's write in our code for this example. 130 00:07:46,120 --> 00:07:47,470 Only one, two, three, four and five. 131 00:07:50,070 --> 00:07:52,650 So I have one, two, three, four, five. 132 00:07:55,870 --> 00:07:58,000 So I will call you guys in on one, two, three, four. 133 00:08:00,800 --> 00:08:06,950 Then I will call that equation one, two, three, then I will call Erica Jong un went to. 134 00:08:08,500 --> 00:08:10,150 Then I call that equation on one. 135 00:08:11,600 --> 00:08:15,240 So a number of elements went ahead and then I recall that equation ampitheater. 136 00:08:15,260 --> 00:08:16,710 So basically a number of elements. 137 00:08:16,790 --> 00:08:19,730 Zero seven zero zero. 138 00:08:20,090 --> 00:08:24,050 So I will return zero to a small answer is basically zero. 139 00:08:24,320 --> 00:08:26,750 You will find out the last digit last digit is one. 140 00:08:27,020 --> 00:08:29,300 So zero multiplied by ten plus one. 141 00:08:29,390 --> 00:08:30,560 So I will return one. 142 00:08:31,980 --> 00:08:39,270 So Lastminute is basically two Los Angeles two, so one multiplier 10 plus two, so I will return to 143 00:08:39,270 --> 00:08:46,710 El Lastminute this basically so 20 multiplied 10 plus three, so I will return 123. 144 00:08:47,550 --> 00:08:49,170 So last digit is basically four. 145 00:08:49,170 --> 00:08:50,760 So small answer is 123. 146 00:08:51,150 --> 00:08:56,100 Smolan is 123 so small and multiply digit so multiply 10. 147 00:08:56,440 --> 00:08:57,530 LaserJet is four. 148 00:08:57,540 --> 00:08:59,730 So I will return one, two, three, four. 149 00:09:01,520 --> 00:09:07,070 So small answer is one, two, three, four, lastminute is five, so multiply by ten and plus five. 150 00:09:07,250 --> 00:09:11,670 So you've done one, two, three, four and five and that is the correct answer. 151 00:09:12,200 --> 00:09:13,890 So that's how our function is working. 152 00:09:14,060 --> 00:09:18,160 So right now, our function will not work for cases like minus 123. 153 00:09:18,980 --> 00:09:21,410 So this is a valid integer, right? 154 00:09:21,440 --> 00:09:26,450 So if there's a string, if this thing is minus 123, then your output should be minus 123. 155 00:09:26,690 --> 00:09:28,450 But our code will not handle this. 156 00:09:28,760 --> 00:09:35,560 Similarly, if nothing is plus 123, so the digital part will be 123. 157 00:09:35,570 --> 00:09:37,450 So currently our code will not handle this. 158 00:09:37,730 --> 00:09:43,430 Similarly, if the input string is wrong, for example, the inputs doing this, one, two, maybe three, 159 00:09:43,430 --> 00:09:43,700 four. 160 00:09:44,000 --> 00:09:47,840 So if this is the string, then you cannot convert the string into the integer. 161 00:09:47,910 --> 00:09:53,870 OK, so this string cannot be converted into an integer and our function will not handle this case also. 162 00:09:54,200 --> 00:09:56,060 So our function has a lot of limitation. 163 00:09:56,060 --> 00:09:58,370 It will work only for specific inputs. 164 00:09:58,520 --> 00:10:04,300 OK, so for example, string C, you are putting string so string can be very, very long. 165 00:10:04,310 --> 00:10:09,470 So if this is our string, one, two, three, four, five, six, seven, eight, nine, one, two, 166 00:10:09,470 --> 00:10:13,070 three, four, five, six, seven, eight, nine and so on. 167 00:10:13,220 --> 00:10:16,790 So string can contain as many number of characters as you'd want. 168 00:10:17,000 --> 00:10:18,630 So string can be very, very large. 169 00:10:19,490 --> 00:10:22,270 So can you convert this string into an integer? 170 00:10:22,340 --> 00:10:22,750 No. 171 00:10:22,970 --> 00:10:25,220 So Integer has basically a very small range. 172 00:10:25,460 --> 00:10:30,350 OK, you are returning integer so you cannot convert this big number into an integer. 173 00:10:30,860 --> 00:10:34,900 OK, so this type of string, a very long string, cannot be converted into an integer. 174 00:10:35,540 --> 00:10:42,500 So I think an integer you can store around nine digits, we can store around nine digits and not more 175 00:10:42,500 --> 00:10:43,310 than nine digits. 176 00:10:43,550 --> 00:10:50,810 If you want to store it in digits, you need to use that the type longlong, but our string can contain 177 00:10:50,810 --> 00:10:52,940 as many number of characters as it wants. 178 00:10:53,120 --> 00:10:59,360 So if this thing is containing 20 letters containing 20 characters or let's say two hundred characters, 179 00:10:59,480 --> 00:11:04,400 then this cannot be converted into an integer so that a function will not work for those cases also. 180 00:11:04,730 --> 00:11:08,540 So our function will work only for small inputs and for right inputs. 181 00:11:08,600 --> 00:11:11,290 OK, so this is from this video. 182 00:11:11,330 --> 00:11:12,530 I will tell you the next one.