Friday, December 27, 2024
Google search engine
HomeLanguagesPython – Reverse a words in a line and keep the special...

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:
                 
                # If it is special character, then
                # reverse non special characters and
                # append special character
                 
                rev_sub_word += str_word[::-1] + ch
                 
                # Clear the old stached character, as
                # it is already reversed
                str_word = ''
         
        # Keep appending each words, also add words
        # ending with non-special character
        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.

  1. Define the function reverse_words which takes a string as input.
  2. Create a regular expression pattern to split the input string into words and delimiters.
  3. Split the input string into words and delimiters using the pattern.
  4. Reverse each word in the list of words and create a new list of reversed words.
  5. Join the reversed words and delimiters together to form the output string.
  6. Return the output string.

Python3




# Python program for the above approach
import re
 
# Function to reverse the given words
def reverse_words(line):
 
    # Define a pattern
    pattern = re.compile(r'\W+')
    words = pattern.split(line)
    delimiters = pattern.findall(line)
    reversed_words = [word[::-1] for word in words]
 
    # Logic for reverse
    reversed_line = ''.join(
        [word + delimiter for word, delimiter in zip(reversed_words, delimiters + [''])])
 
    # Return the result
    return reversed_line
 
 
# Driver Code
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.

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments