The Art of Reversing a String with Pointers
When it comes to writing efficient code, pointer operations can be both powerful and tricky to manage. One such task that requires careful handling of pointers is reversing a string. In this article, we will delve into the world of pointer arithmetic and learn how to reverse a string using this technique.
Managing Pointer Arithmetic
-------------------------
Pointer arithmetic is a fundamental concept in C programming language. It allows us to manipulate memory addresses using mathematical operations. When working with strings, we often need to access individual characters or words, which can be achieved through pointer arithmetic. In the context of reversing a string, pointer arithmetic plays a crucial role.
The key to reversing a string lies in understanding how to increment and decrement pointers. To reverse a word, we need to start from the end of the word and move backwards to the beginning. This can be achieved by setting two pointers: one starting at the beginning of the word (`word_start`) and another starting at the end of the word (`temp`). We then increment `temp` until it points to the first character of the string, which marks the start of a new word.
Swapping Pointers
----------------
When we encounter the end of a word, we need to reverse it. To do this, we swap the values of `word_start` and `temp`. This effectively reverses the order of characters in the word. We then update `word_start` to point to the next character, which is `temp + 1`.
Reversing Strings
-----------------
Now that we have mastered the art of swapping pointers, we can move on to reversing strings. To reverse a string, we need to iterate through each character and split it into individual words. We use two pointers: one starting at the beginning of the word (`word_start`) and another starting at the end of the word (`temp`). We then swap these pointers using the technique discussed earlier.
Reversing Words
----------------
When we encounter a blank space, we know that the first word is complete. In this case, we need to reverse the previous word before moving on to the next one. To do this, we set `word_start` to point to the last character of the current word (`temp - 1`) and swap it with the original value of `temp`. This effectively reverses the order of characters in the word.
Final Reversal
----------------
Once we have reversed all words, we need to reverse the entire string. To do this, we set our starting pointer (`s`) to point to the first character of the string (`temp - 1`). We then call a function `reverse` that takes care of reversing the string from the start.
Conclusion
----------
Reversing a string using pointers requires careful handling of pointer arithmetic and a solid understanding of how to increment and decrement pointers. By mastering these techniques, we can write efficient code in any programming language. The key takeaway from this article is the importance of attention to detail when working with pointers, as even small mistakes can lead to incorrect results.
While the task of reversing a string may seem trivial, it requires careful consideration of edge cases and pointer arithmetic. By following the steps outlined in this article, readers should be able to write efficient code that reverses strings using pointers.
"WEBVTTKind: captionsLanguage: enso here is a very simple problem around drinks right so the problem here is as follows given a string like this okay it's called reversing the words in a given string remember it is not reversing the string that's a much simpler problem this is slightly trickier than simply reversing a strength right so imagine if I am given a string like this I love algorithms right I want to reverse in such a way that this I want to reverse the words in the string so I want the last word to become the first word I want the last but one to become the second from the first and I want this to become last look at this I'm not reversing the whole string I'm not my resultant string is not sm h t I R so on so forth it's not that I want to reverse the individual words in a given strength slightly more tricky again you whichever programming language you are using again the solution to this depends a lot on the programming lung language constructs you have if you of course it is toughest to write it in C or C++ if you are writing it in Java or Python or any other any other more model language which has explicitly string manipulation libraries it's much much easier if you have if you are using if you are using Java or Python because they have string manipulation libraries there are simple straightforward functions to give in a string to cut it into individual words it's much more easy in Java and Python right it's slightly harder in C and C++ because you have to because in C and C++ these strings are refer or thought of as simply you are a character Alice right so I'll show you a solution in 3c C and C++ but if you are solving this using Java and Python it should be much much easier than C C++ okay so this is very program specific very programming language specific solution that I am giving but the general idea you can easily implement in Java or Python if you know the string libraries for or the string functions for Java and Python right so now again before we go there please pause this video and try it whichever programming language you are choosing and if you are choosing C C++ be very very careful with the pointer logic it's it's doable it's not rocket science the solution is fairly simple if the length of the string is n the length of the string is n we can easily construct an order of n algorithm okay I will be also discussing just an order of n time algorithm itself right it's fairly simple it's not rocket science okay you just have to be careful with this string if you're writing code in C or C++ you have to be careful with the pointer arithmetic and the pointer manipulations that's all there is if you're writing in Java and Python you just have to know the correct functions or this correct classes and functions that you have to use from the string libraries that's all there is okay so please try it before you check out the rest of the solution okay so the strategy that I employ is this first let's think of the strategy that will employ again you could solve this problem in many many ways this is just one of the many possible ways the solution that I was thinking of is michaelis imagine if I have a string like this what I'll do is I will take individual words in this I will take this word I will reverse it so reverse individual words then I take this word and I'll reverse this because I am reversing this word then I take this word and I will reverse it how do I know that this is one word again I try to write code in C or C++ right using pointer logic right so are using using pointer operations so in c programming language if i have if i have a blank space then i know that it is a separator between two words the end of the string I know is backslash zero okay this this these are very c c++ specific stuff so i will use the fact that you have a blank space between every word so now I will cut each word and I'll reverse this individual words I reverse this individual words and then use a function called reverse to in to be to reverse each of these individual words now once I get this string what I do now is I reverse this whole string now if I reverse this whole student what happens if I reverse this whole string not word by word we can his first I reverse to the word that I reverse the whole string now if i reverse the whole stream mode i get i get algorithms love i that's the output that I want okay this is this is one of the many strategies that you can employ right so this is one of the strategies that I thought I will employ and let me walk you through the C C++ code again if you're planning to write code in Java peyten that's perfectly okay right so let's see the secret because C code is the trickiest because of the point and logic so I have a function called reverse words which does the whole reversal okay so this reverse word takes a character pointer okay you again this is a pointer to the string so if you give me this string it points to its a character pointer pointing to the first character here okay that that's my input okay simple now let's go step by step I'll walk you through this whole program now I'd say so first of all first of all what I need to do here is I need to find individual words and reverse them right so first I start breaking down the big string that is given to me in two words but my first word start will be the same as s remember s is a pointer here let's not forget that okay you might imagine if my string was let's say love algorithms let's assume this is the only stream there is no I because reversing I doesn't make any sense so my S is a pointer to this my S is a pointer to this so I make my word start also point to this okay let's call it WS my word start also points to this my temp also points to this love my TMP is also a character pointer pointing to this okay the reason I am NOT taking the string I love algorithms because I is a single character word there is no point in reversing it it's straightforward now what I do here is this while star 10 look at this I'll keep moving my temp in the time I reach the end of the string till the time I reach end of strings I'll keep going ahead that's why I have well while star temp which means while the data that is stored at temp is not equals to backslash 0 that's what this means in C programming language I'll keep incrementing my 10th I will keep incrementing my temp now what I do now is this if star temp is not equals to backslash 0 remember if it is bad if it is equals to backslash 0 that means I've reached the end if I've reached the end I need to reverse the last word so I have a function here called the reverse remember I have a function here called the reverse I also talked to you about the reverse function reverse function takes two pointers the starting point and the ending point and it reverses this whole word okay so the reverse function looks like this let's let's understand the reverse function so that understanding the reverse word becomes easier okay the reverse function looks like this it's a function wherein you give the starting point ER or the beginning point and end point imagine I have a word like this this is beginning point if this is the end point I'll just use a temporary storage this is like simply swapping I have to swap this end this then I have to swap this end this then I swap this with itself right I actually I can skip this okay I have this pointers beginning and end that's the logic here very simple logic so I'll say when B is less than e obviously this is a pointer this is the point that B is less than e I'll store whatever is stored in star begin star begin means value stored at the begin pointer okay so instead of calling it begin let me just call it B okay I think in my notes okay so let me just call it B off making it simpler okay okay okay okay so we are here okay so I just forgot to wrote and wrote begin here when I am writing the pseudocode it's actually be right temp a star B so in temp I am storing my ex in temp I'm storing X now I am staring now I'd say value stored at B after which I have a plus plus value stored at B is same as value stored at E which means this M will move here so this becomes n enough and the B gets incremented look at this star B and this is a post increment right so this B will now point to the second character right look at this B will not point to the second character now I am saying star e equals to temp which means even love 1/2 X E will now have X now look at this these two got swapped off and we are doing II - - so which means evil point to this again this is very specific to C programming language so what did we do we had XYZ mmm we swapped these two and we changed the beginning pointer here we change the end pointer here we made this n we made this X and we keep doing it till a time B is less than e this way given two pointers the beginning and end pointer I can keep swapping them one after the other this is how my reverse function works now look at this what am I saying here if I encounter the end of the string if I encounter the end of the string whatever is my word start my word start will always point to the last word that I have if I'm if I'm looking at the end of the string here look in this if I'm looking at the end of the string here if I'm looking at the end of the string what happens now my starting so then the last word which is that needs to be reversed look at this if my temp points to this if my temp is pointing to this then my word start will point to this then what do I want to do I want to reverse the last word so I will say reverse word start temp minus 1 so this is your temp minus 1 right again this is pointer arithmetic here because this is a character it will subtract what so given that is minus 1 you're going to point to the previous character it very simple logic so this is handling the case where where I am processing the last word in inner strength ok else if my star tent is blank space suppose imagine I keep I keep incrementing this temp look at this I keep incrementing this temp here my word start points to this I keep incrementing the temp my temp will reach here at some point so the moment I encounter a black space that means my first word is done so what do I do now I reverse from the word start to temp minus 1 so I reverse this word I reverse this word if it is not end of string also whenever I find a blank space I have to reverse that word I reverse that word we can assign reverse that word and my word start that this word is reverse right then my word start has to point to M plus 1 because I have to start looking at the next word now that's my mom word start will become 10 plus 1 and that's it ok this while loop will keep going on this while loop will keep going on till the time my temp reaches the end of the string so by the end of this see look at this by the time of this while loop look at the by the time this while loop ends what do we have we literally have this we literally have this that's what happens by the end of this while loop now once once we have that what do we want to do I want to reverse this whole thing that's why I'm calling rivers I am calling the whole thing with rivers which is pointing to the first string s is always going to point to this my temp minus 1 because my temp is pointing here at the end of the string at the end of this side the end of this whole while loop I come to the end of the string so my temp minus 1 will point to this so I am reversing the whole thing that's it we can this there are again the steps here are very very important if you are writing your code in C or C++ using pointed operations you have to manage your pointer operations very very carefully and even the reverse part you have to be careful with the pointer operations again if you are using a different programming language you will have to use the functions that you have for string manipulations very carefully but the logic remains the same you take individual words reverse these individual words and then reverse the whole string that's the concept the reason I read oh I wrote the code in C programming language is because the pointer operations are slightly dirty that you have to be careful about you have to handle both the cases where the string is ending and where the word is ending both these cases have to be a handle gracefully right that's it it's it's not rocket science it's a very simple thing I would actually not call this task more as a programming or did us so I do not call this as a data structure or algorithm task I call it more of a programming task okay questions like this are typically asked to know whether you can write reasonably good code in the programming language of your choice see if you can pick Java Python whatever language you want for most product based companies can you write slightly tricky code in the programming language of your choice because the there is no there is nothing fancy or complex here as far as time complex these concern the time complex is order of M typically these types of questions are asked in the first phone screen or in the first interview to see whether you can code again writing this code is not trivial you have to be careful to handle all the cases and to write the point and operations very very carefully okay it's not trivial so it is testing your knowledge of programming or your ability to program in any programming language of your choice more than your tests of algorithms and data structures actually okayso here is a very simple problem around drinks right so the problem here is as follows given a string like this okay it's called reversing the words in a given string remember it is not reversing the string that's a much simpler problem this is slightly trickier than simply reversing a strength right so imagine if I am given a string like this I love algorithms right I want to reverse in such a way that this I want to reverse the words in the string so I want the last word to become the first word I want the last but one to become the second from the first and I want this to become last look at this I'm not reversing the whole string I'm not my resultant string is not sm h t I R so on so forth it's not that I want to reverse the individual words in a given strength slightly more tricky again you whichever programming language you are using again the solution to this depends a lot on the programming lung language constructs you have if you of course it is toughest to write it in C or C++ if you are writing it in Java or Python or any other any other more model language which has explicitly string manipulation libraries it's much much easier if you have if you are using if you are using Java or Python because they have string manipulation libraries there are simple straightforward functions to give in a string to cut it into individual words it's much more easy in Java and Python right it's slightly harder in C and C++ because you have to because in C and C++ these strings are refer or thought of as simply you are a character Alice right so I'll show you a solution in 3c C and C++ but if you are solving this using Java and Python it should be much much easier than C C++ okay so this is very program specific very programming language specific solution that I am giving but the general idea you can easily implement in Java or Python if you know the string libraries for or the string functions for Java and Python right so now again before we go there please pause this video and try it whichever programming language you are choosing and if you are choosing C C++ be very very careful with the pointer logic it's it's doable it's not rocket science the solution is fairly simple if the length of the string is n the length of the string is n we can easily construct an order of n algorithm okay I will be also discussing just an order of n time algorithm itself right it's fairly simple it's not rocket science okay you just have to be careful with this string if you're writing code in C or C++ you have to be careful with the pointer arithmetic and the pointer manipulations that's all there is if you're writing in Java and Python you just have to know the correct functions or this correct classes and functions that you have to use from the string libraries that's all there is okay so please try it before you check out the rest of the solution okay so the strategy that I employ is this first let's think of the strategy that will employ again you could solve this problem in many many ways this is just one of the many possible ways the solution that I was thinking of is michaelis imagine if I have a string like this what I'll do is I will take individual words in this I will take this word I will reverse it so reverse individual words then I take this word and I'll reverse this because I am reversing this word then I take this word and I will reverse it how do I know that this is one word again I try to write code in C or C++ right using pointer logic right so are using using pointer operations so in c programming language if i have if i have a blank space then i know that it is a separator between two words the end of the string I know is backslash zero okay this this these are very c c++ specific stuff so i will use the fact that you have a blank space between every word so now I will cut each word and I'll reverse this individual words I reverse this individual words and then use a function called reverse to in to be to reverse each of these individual words now once I get this string what I do now is I reverse this whole string now if I reverse this whole student what happens if I reverse this whole string not word by word we can his first I reverse to the word that I reverse the whole string now if i reverse the whole stream mode i get i get algorithms love i that's the output that I want okay this is this is one of the many strategies that you can employ right so this is one of the strategies that I thought I will employ and let me walk you through the C C++ code again if you're planning to write code in Java peyten that's perfectly okay right so let's see the secret because C code is the trickiest because of the point and logic so I have a function called reverse words which does the whole reversal okay so this reverse word takes a character pointer okay you again this is a pointer to the string so if you give me this string it points to its a character pointer pointing to the first character here okay that that's my input okay simple now let's go step by step I'll walk you through this whole program now I'd say so first of all first of all what I need to do here is I need to find individual words and reverse them right so first I start breaking down the big string that is given to me in two words but my first word start will be the same as s remember s is a pointer here let's not forget that okay you might imagine if my string was let's say love algorithms let's assume this is the only stream there is no I because reversing I doesn't make any sense so my S is a pointer to this my S is a pointer to this so I make my word start also point to this okay let's call it WS my word start also points to this my temp also points to this love my TMP is also a character pointer pointing to this okay the reason I am NOT taking the string I love algorithms because I is a single character word there is no point in reversing it it's straightforward now what I do here is this while star 10 look at this I'll keep moving my temp in the time I reach the end of the string till the time I reach end of strings I'll keep going ahead that's why I have well while star temp which means while the data that is stored at temp is not equals to backslash 0 that's what this means in C programming language I'll keep incrementing my 10th I will keep incrementing my temp now what I do now is this if star temp is not equals to backslash 0 remember if it is bad if it is equals to backslash 0 that means I've reached the end if I've reached the end I need to reverse the last word so I have a function here called the reverse remember I have a function here called the reverse I also talked to you about the reverse function reverse function takes two pointers the starting point and the ending point and it reverses this whole word okay so the reverse function looks like this let's let's understand the reverse function so that understanding the reverse word becomes easier okay the reverse function looks like this it's a function wherein you give the starting point ER or the beginning point and end point imagine I have a word like this this is beginning point if this is the end point I'll just use a temporary storage this is like simply swapping I have to swap this end this then I have to swap this end this then I swap this with itself right I actually I can skip this okay I have this pointers beginning and end that's the logic here very simple logic so I'll say when B is less than e obviously this is a pointer this is the point that B is less than e I'll store whatever is stored in star begin star begin means value stored at the begin pointer okay so instead of calling it begin let me just call it B okay I think in my notes okay so let me just call it B off making it simpler okay okay okay okay so we are here okay so I just forgot to wrote and wrote begin here when I am writing the pseudocode it's actually be right temp a star B so in temp I am storing my ex in temp I'm storing X now I am staring now I'd say value stored at B after which I have a plus plus value stored at B is same as value stored at E which means this M will move here so this becomes n enough and the B gets incremented look at this star B and this is a post increment right so this B will now point to the second character right look at this B will not point to the second character now I am saying star e equals to temp which means even love 1/2 X E will now have X now look at this these two got swapped off and we are doing II - - so which means evil point to this again this is very specific to C programming language so what did we do we had XYZ mmm we swapped these two and we changed the beginning pointer here we change the end pointer here we made this n we made this X and we keep doing it till a time B is less than e this way given two pointers the beginning and end pointer I can keep swapping them one after the other this is how my reverse function works now look at this what am I saying here if I encounter the end of the string if I encounter the end of the string whatever is my word start my word start will always point to the last word that I have if I'm if I'm looking at the end of the string here look in this if I'm looking at the end of the string here if I'm looking at the end of the string what happens now my starting so then the last word which is that needs to be reversed look at this if my temp points to this if my temp is pointing to this then my word start will point to this then what do I want to do I want to reverse the last word so I will say reverse word start temp minus 1 so this is your temp minus 1 right again this is pointer arithmetic here because this is a character it will subtract what so given that is minus 1 you're going to point to the previous character it very simple logic so this is handling the case where where I am processing the last word in inner strength ok else if my star tent is blank space suppose imagine I keep I keep incrementing this temp look at this I keep incrementing this temp here my word start points to this I keep incrementing the temp my temp will reach here at some point so the moment I encounter a black space that means my first word is done so what do I do now I reverse from the word start to temp minus 1 so I reverse this word I reverse this word if it is not end of string also whenever I find a blank space I have to reverse that word I reverse that word we can assign reverse that word and my word start that this word is reverse right then my word start has to point to M plus 1 because I have to start looking at the next word now that's my mom word start will become 10 plus 1 and that's it ok this while loop will keep going on this while loop will keep going on till the time my temp reaches the end of the string so by the end of this see look at this by the time of this while loop look at the by the time this while loop ends what do we have we literally have this we literally have this that's what happens by the end of this while loop now once once we have that what do we want to do I want to reverse this whole thing that's why I'm calling rivers I am calling the whole thing with rivers which is pointing to the first string s is always going to point to this my temp minus 1 because my temp is pointing here at the end of the string at the end of this side the end of this whole while loop I come to the end of the string so my temp minus 1 will point to this so I am reversing the whole thing that's it we can this there are again the steps here are very very important if you are writing your code in C or C++ using pointed operations you have to manage your pointer operations very very carefully and even the reverse part you have to be careful with the pointer operations again if you are using a different programming language you will have to use the functions that you have for string manipulations very carefully but the logic remains the same you take individual words reverse these individual words and then reverse the whole string that's the concept the reason I read oh I wrote the code in C programming language is because the pointer operations are slightly dirty that you have to be careful about you have to handle both the cases where the string is ending and where the word is ending both these cases have to be a handle gracefully right that's it it's it's not rocket science it's a very simple thing I would actually not call this task more as a programming or did us so I do not call this as a data structure or algorithm task I call it more of a programming task okay questions like this are typically asked to know whether you can write reasonably good code in the programming language of your choice see if you can pick Java Python whatever language you want for most product based companies can you write slightly tricky code in the programming language of your choice because the there is no there is nothing fancy or complex here as far as time complex these concern the time complex is order of M typically these types of questions are asked in the first phone screen or in the first interview to see whether you can code again writing this code is not trivial you have to be careful to handle all the cases and to write the point and operations very very carefully okay it's not trivial so it is testing your knowledge of programming or your ability to program in any programming language of your choice more than your tests of algorithms and data structures actually okay\n"