Python – Reverse a words in a line and keep the special characters untouched
Reverse the characters in all words in a line including numbers, but leave special characters and symbols untouched in the same position. Consider the below examples.
Input: ‘Bangalore is@#$!123 locked again in jul2020’ should change to
Output: ‘erolagnaB si@#$!321 dekcol niaga ni 0202luj’
Input: ‘Bangalore is@#$!123locked locked again in jul2020’ should change to
Output: ‘erolagnaB si@#$!dekcol321 dekcol niaga ni 0202luj’
Look at the above example, each word is reversed, if there is any special character, then the word surrounding the special character gets reversed.
Python3
def reverStringsInLine(s):
sl = s.split( ' ' )
rsl = ''
for word in sl:
str_word = ''
rev_sub_word = ''
for ch in word:
if ch.isalnum():
str_word + = ch
else :
rev_sub_word + = str_word[:: - 1 ] + ch
str_word = ''
r_word = rev_sub_word + str_word[:: - 1 ]
rsl + = r_word + ' '
return rsl
s = 'Bangalore is@#$!123locked locked again in jul2020'
print (reverStringsInLine(s))
|
Output
erolagnaB si@#$!dekcol321 dekcol niaga ni 0202luj
Time Complexity: O(n2)
Auxiliary Space: O(n)
Approach#2: Using Regular Expressions: This approach takes the input string and splits it into words using regular expression. It also finds the delimiters that separate the words. The words are then reversed while keeping the delimiters in their original position. Finally, the reversed words and delimiters are joined together to form the output string.
- Define the function reverse_words which takes a string as input.
- Create a regular expression pattern to split the input string into words and delimiters.
- Split the input string into words and delimiters using the pattern.
- Reverse each word in the list of words and create a new list of reversed words.
- Join the reversed words and delimiters together to form the output string.
- Return the output string.
Python3
import re
def reverse_words(line):
pattern = re. compile (r '\W+' )
words = pattern.split(line)
delimiters = pattern.findall(line)
reversed_words = [word[:: - 1 ] for word in words]
reversed_line = ''.join(
[word + delimiter for word, delimiter in zip (reversed_words, delimiters + [''])])
return reversed_line
line = 'Bangalore is@#$!123 locked again in jul2020'
reversed_line = reverse_words(line)
print (reversed_line)
line = 'Bangalore is@#$!123locked locked again in jul2020'
reversed_line = reverse_words(line)
print (reversed_line)
|
Output
erolagnaB si@#$!321 dekcol niaga ni 0202luj
erolagnaB si@#$!dekcol321 dekcol niaga ni 0202luj
Time Complexity: O(n), where n is the length of the input string. This is because the code iterates over each character in the input string exactly once.
Space Complexity: O(n). This is because the code creates a list of words, a list of delimiters, and a list of reversed words, all of which have a maximum length of n. The code also creates a new string to hold the reversed words and delimiters, which also has a maximum length of n.
Last Updated :
21 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...