String manipulation in recursion


how can i use (s+1, n-1, key) where s is string without raising an error?

@goyalvivek you cannot use s+1 for a string. When s is an array, its holding an address(address of the first character), like a pointer. So when you do s+1 there, it will increment the address and you will reach the next character in the array.
If s was an int variable, doing s+1 would incremement its value.
If s was a char value, doing s+1 would convert the char to its ASCII value, increment the ASCII value and give the corresponding character instead.
Now think about it, if s is a string, what do you expect s+1 to do? Its not holding an address its not an array, its a simple variable holding a bunch of characters. So, this operation is not supported for string datatypes.

I hope you understood why this thing is not possible now.

so for string manipulation i’ll have to take another string in every question and store my answer in it as i iterate the original (input) string right?

@goyalvivek you can manipulate this string also.
For eg if you wish to delete an element in the string, you could use the string::erase(idx, len) function which will remove the elements starting at idx index, upto length len.

I am unable to apply string manipulation in my code, https://ide.codingblocks.com/s/588579 please help me correct it and add comments for more understanding

@goyalvivek i have explained the logic in code itself https://ide.codingblocks.com/s/588582

Got it, couldn’t have explained better. one more question, just like we can delete index/indexes from a string, can we add a char inside a string like VVEK TO ViVEK? in recursion?

@goyalvivek yes you can use the string::insert function for that. C++ STL is very vast.

Understood! Thanks for ur help.

1 Like

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.