Implement strrev is a possible interview question. How would you do it ?
The simple way is to go to the back of the string, allocate a buffer and push the character from the right to left into the buffer and return it.
But the above approach use extra space
How would you do without using this extra space?
Just swap the last element in the string with the first element until you reach the middle.
- if char* ptr=”rajan” is used, the above program will crash since, “rajan” is a const literal present part of code segment which cannot be modified.
- strdup creates a new pointer from the input const char*. We use that to remove the const
- free on ptr should be called !!