 What's up guys my name is Michael and welcome to my youtube channel today We're gonna do another leak code challenge called reverse words in a string 3 So you're given a string and you need to reverse the ordering of the characters in each word within a sentence while still preserving the white space in the initial order so in this case We have let's take leak code contest all the values of let's take leak code contest is in the same order But the letters for each word is now reversed now reversed. They're in a reversed order So now the essentially is each word is separated by a single space But it has the same order, but it's reversed the letters are reversed so I'm going to explain to you how I would do this question and Basically, I haven't done this question yet But it's gonna be almost exactly like the previous video that I just showed you Essentially is what we need to do is create an ice stream stream and basically separate every single word in by its spaces Then for each word that we get what we're gonna do is we are going to call the reverse method Onto the string and then concat take that word then we create a new string That was a string that we're returning to return and concatenate it onto that So every time you do you basically reversing the string Concatenate it into the string or returning out of space Reversing the string concatenated to the string you're returning out of space You keep going until you have no more words in your string. Okay, so what I'm gonna do is I'm gonna do that right now Keep in mind guys that I couldn't make a bunch of mistakes here because I'm just doing it with you guys Okay, so I'm gonna call create a string stream. I string I string stream call. I assess that passes in s What I'm gonna do is I'm gonna create a string called each word each word I'm gonna set this to empty. So and then a while each word. I'm going to do while I assess I'm going to essentially Read in each word. Okay, so then what I'm going to this is going to separate each word into its unique Word, okay, it's gonna separate each every single Every single word inside the string by the spaces, so it's gonna have let's take the code contest These are now separate words each that that each Individual words right now. I'm gonna create a string called to return to return is gonna be my final string that I am Essentially going to return into reverse words. That's what I'm going to do So I'm gonna return to return now for each each word what I'm going to do is I'm going to call the reverse method of a string Now I forgot the reverse method of a string But essentially is is that string reverse method is basically just going to return all the It's gonna reverse all the values in the string in reverse order So in this case the string reverse who actually here it actually takes an iterator, which is very strange Let's see if we could find one for strings Let's see reversing a string. Otherwise we have to write our own reverse method, which is something I don't want to do. Okay, so you could just call as TD reverse Copy begin copy and okay, so all I have to do essentially is just create a as to call as TD Reverse and I pass in each word begin and then each word and So that's going to reverse all the words How I think it's going to do is it's going to reverse car copy begin copy and and Watch so let's see. Oh, let's see. Oh, did it did it? Reverse okay, so there's a way to do this without okay. Yeah Let me just see another example just to see if it actually does work the way I intended to because if it doesn't work then We have problems here because then then Because I don't want to actually create my own reverse method, which we could do but I don't want to do that Let's see reverse a string in C++. There should be a way to do this Okay, so you could do you're just playing reversing the happening reversing the string never reverse you use Okay, reverse name. So okay, so to create use a C out. Okay, go to that. Okay, so Reverse underscore name is Okay, so I'm actually going to use this method you are first to spend the reverse string Wait, what is the difference between this? Actually, we'll try both of them and see if this works or not because if it does work then essentially as well it'll work so I'm going to create a new string called to add and I'm going to point this to the reverse string of each word What I'm going to do now is I'm going to do to return is now equal to to return plus To add and I'm going to concatenate a space. That's what I'm going to do Okay, so I'm actually going to concatenate to add and then I'll If it's not the end then I'll concatenate a space. So if the string in the end if Let's see. How am I going to do that? Huh? That's a good question. Okay So in order to check if in order to check how many words there are I have to actually Have to actually add a space space only if only if the Only if I'm didn't reach the end Okay, so I have to add a space only if I didn't reach the end and If I do reach the end I don't add a space That's basically how I have to do this But let's see. Oh, let's just see what this returns because I don't even know what this returns honestly. Let's run Okay, so this is a void function. Okay, so the we know that std reverse is actually a void function So maybe I don't actually have to create a to add I could just do each word So essentially what I think this is going to do is it's going to add each word in its reversed Format, but it's not gonna have any space. Oh, but it's gonna have spaces. Okay, so, okay, so Okay, okay Yeah, okay, so it did what I thought it would do Problem is is that now that you have a space extra space in the end and what I'm going to do is I'm going to remove that space So I'm actually going to do a to room to return is going to equal to to return dot substring from 0 to 0 to to return that size Minus 1 If I recall correctly. Yeah, okay, so I'm going to do to return Equals to return that substring to return that size minus 1 so I'm this is going to sub the is Basically going to I'm gonna start from 0 and I'm going to essentially only take the The the number of letters which is the original size minus 1 so that gets rid of the space Let's run the code again. Let's see if this works Okay, so it got rid of the space that submit and it passed. Holy crap. Okay, so that I didn't expect that to work But okay, basically that worked and yeah, that's how you do it. Let's see you how other people did it Let's see the solution actually so a simple solution Okay, so they did what I did Without using predefined split and reverse Okay, so they wrote their own split which sounds Okay, so they use a string builder here Okay, it looks like all the complexity is O of n So I think this is the only way to do it honestly What I think is the only way to do it So, yeah, right calm subscribe. I'll check you guys later Let's see if there's another person who did faster Okay, so he did Okay, so he did the exact same thing as I did Well, you know, he's asking a question Okay, okay, I don't I don't know what he's doing I thought that was a better solution than me. But yeah, this is how I would do it And a boy to make this better is actually just to Only add the space when you reach the end, but then that I think that would require going through twice Another thing we could call trim But that's the same thing as substringing zero to size minus one size minus one removes the last space because you're only Substringing zero to space. But yeah, anyway, right calm subscribe. I hope you guys enjoyed this video. It's just pretty fast reverse words