How To Reverse A String Without Affecting Special Characters

satyabrata pal
ML and Automation
Published in
3 min readNov 28, 2019

This will be one of my quick post on solving interview coding problems. This time let‘s try to swap alphabets in a string without affecting the special characters which are present in that string.

The Data

string = "l@@me!be*your@@hero%"

Now, we need to reverse the alphabets in such a way that the special characters are untouched. Such that the reversed string would appear as this →

'o@@re!hr*uoye@@beml%'

The Algorithm

  • Traverse through the string till the left pointer is less than the right pointer i.e. till the entire string is traversed.
  • Check the characters which are available both on the left side of the staring as well as on the right side of the string.
  • For every character encountered on the right side check if that character is an alphabet.
  • Do this check for the characters on the left side as well.
  • If the characters on the left side is not an alphabet then move to the next element.
  • Do this for the character encountered on the right side as well.
  • If the character (either on the left side or the right side) is an alphabet then swap the right character with the left character.

The Program

def reverseString(string):

toList = list(string)

rightPointer = len(toList)-1
leftPointer = 0

while leftPointer < rightPointer:
if not toList[leftPointer].isalpha():
leftPointer +=1
elif not toList[rightPointer].isalpha():
rightPointer -=1
else:
toList[leftPointer], toList[rightPointer] = toList[rightPointer], toList[leftPointer]

leftPointer +=1
rightPointer -=1

return ''.join(toList)
  • rightPointer is the index of the character on the right side.
  • leftPointer is the index of the character on the right side.

After defining the rightPointer & leftPointer we traverse the list from left as well as right. If the leftPointer is not an alphabet then we increment it by 1 so that we move to the next index. If rightPointer is not an alphabet then we decrease it by 1 so that we move to the previous element. If either the leftPointer or rightPointer is an alphabet then we swap the character at leftPointer with the character at rightPointer .

Finally, we join the elements of the toList to form a string and then we return it in our output.

Conclusion

I don’t have much time to write a concluding sentence for this article as I am in a hurry since I am writing this in my office before packing up for the day. The only thing I would say is that try out this code on different types of strings or sentences and have fun.

Additional Resource

Update[10Jan’20]: Recently one of the readers of this article pointed out to me that there is a nice blogpost at toptal.com which I might like to read. I read that blog-post and it was indeed very well written.

This blog post is actually a guide about how hiring the right candidate for the position of python developer. This is a must read if you happen to be a hiring manager. The blog post can be read here.

Announcement

I am so excited to announce that my first deep learning course is now available on Udemy . The course is available at 95% discount till 31st May midnight. Use this link to apply the discount code.

--

--

satyabrata pal
ML and Automation

A QA engineer by profession, ML enthusiast by interest, Photography enthusiast by passion and Fitness freak by nature