Given an array of characters, which is basically a sentence. However, there is no space between different words and the first letter of every word is in uppercase. You need to print this sentence after the following amendments:
- Put a single space between these words.
- Convert the uppercase letters to lowercase
Examples:
Input : BruceWayneIsBatman
Output : bruce wayne is batman
Input : GeeksForGeeks
Output : geeks for geeks
Regex in Python to put spaces between words starting with capital letters
We have an existing solution for this problem, please refer Put spaces between words starting with capital letters link.
We can solve this problem quickly in python using findall() method of re (regex) library.
Approach :
- Split each word starting with a capital letter using re.findall(expression, str) method.
- Now change the capital letter of each word to lowercase and concatenate each word with space.
Implementation:
Python3
import re
def putSpace( input ):
words = re.findall( '[A-Z][a-z]*' , input )
for i in range ( 0 , len (words)):
words[i] = words[i][ 0 ].lower() + words[i][ 1 :]
print ( ' ' .join(words))
if __name__ = = "__main__" :
input = 'BruceWayneIsBatman'
putSpace( input )
|
Output
bruce wayne is batman
Time Complexity: O(n)
Auxiliary Space: O(n)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
31 Jul, 2023
Like Article
Save Article