Put spaces between words starting with capital letters - GeeksforGeeks

25 Jun, 2021
You are 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 following amendments: (i) Put a single space between these words. (ii) Convert the uppercase letters to lowercase.
Examples: 
Input : BruceWayneIsBatman
Output : bruce wayne is batman

Input :  You
Output :  you
We check if the current character is in uppercase then print ” “(space) and convert it into lowercase. 
C++
Java
Python3
C#
Javascript
// C++ program to put spaces between words starting// with capital letters.#include <iostream>using namespace std; // Function to amend the sentencevoid amendSentence(string str){    // Traverse the string    for(int i=0; i < str.length(); i++)    {        // Convert to lowercase if its        // an uppercase character        if (str[i]>='A' && str[i]<='Z')        {            str[i]=str[i]+32;             // Print space before it            // if its an uppercase character            if (i != 0)                cout << " ";             // Print the character            cout << str[i];        }         // if lowercase character        // then just print        else            cout << str[i];    }} // Driver codeint main(){    string str ="BruceWayneIsBatman";    amendSentence(str);    return 0;}
// Java program to put spaces between words starting// with capital letters. import java.util.*;import java.lang.*;import java.io.*; class AddSpaceinSentence{    // Function to amend the sentence    public static void amendSentence(String sstr)    {        char[] str=sstr.toCharArray();                 // Traverse the string        for (int i=0; i < str.length; i++)        {            // Convert to lowercase if its            // an uppercase character            if (str[i]>='A' && str[i]<='Z')            {                str[i] = (char)(str[i]+32);                                 // Print space before it                // if its an uppercase character                if (i != 0)                    System.out.print(" ");                     // Print the character                System.out.print(str[i]);            }                 // if lowercase character            // then just print            else            System.out.print(str[i]);        }    }             // Driver Code    public static void main (String[] args)    {        String str ="BruceWayneIsBatman";        amendSentence(str);    }}
# Python3 program to put spaces between words# starting with capital letters. # Function to amend the sentencedef amendSentence(string):    string = list(string)     # Traverse the string    for i in range(len(string)):         # Convert to lowercase if its        # an uppercase character        if string[i] >= 'A' and string[i] <= 'Z':            string[i] = chr(ord(string[i]) + 32)             # Print space before it            # if its an uppercase character            if i != 0:                print(" ", end = "")             # Print the character            print(string[i], end = "")         # if lowercase character        # then just print        else:            print(string[i], end = "") # Driver Codeif __name__ == "__main__":    string = "BruceWayneIsBatman"    amendSentence(string) # This code is contributed by# sanjeev2552
// C# program to put spaces between words// starting with capital letters.using System;         public class GFG {         // Function to amend the sentence    public static void amendSentence(string sstr)    {        char[] str = sstr.ToCharArray();                 // Traverse the string        for (int i = 0; i < str.Length; i++)        {                         // Convert to lowercase if its            // an uppercase character            if (str[i] >= 'A' && str[i] <= 'Z')            {                str[i] = (char)(str[i] + 32);                                 // Print space before it                // if its an uppercase                // character                if (i != 0)                    Console.Write(" ");                     // Print the character                Console.Write(str[i]);            }                 // if lowercase character            // then just print            else                Console.Write(str[i]);        }    }         // Driver Code    public static void Main ()    {        string str ="BruceWayneIsBatman";                 amendSentence(str);    }         } // This code is contributed by Sam007.
<script>     // JavaScript program to put spaces between words    // starting with capital letters.         // Function to amend the sentence    function amendSentence(sstr)    {        let str = sstr.split('');                   // Traverse the string        for (let i = 0; i < str.length; i++)        {                           // Convert to lowercase if its            // an uppercase character            if (str[i].charCodeAt() >= 'A'.charCodeAt() &&            str[i].charCodeAt() <= 'Z'.charCodeAt())            {              str[i] =              String.fromCharCode(str[i].charCodeAt() + 32);                                   // Print space before it                // if its an uppercase                // character                if (i != 0)                    document.write(" ");                       // Print the character                document.write(str[i]);            }                   // if lowercase character            // then just print            else                document.write(str[i]);        }    }         let str ="BruceWayneIsBatman";                 amendSentence(str);     </script>
Output:  
bruce wayne is batman
This article is contributed by Sahil Chhabra. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
Sam007
sanjeev2552
divyeshrabadiya07
surindertarika1234
Adobe
Strings
Adobe
Strings
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Check for Balanced Brackets in an expression (well-formedness) using Stack
Python program to check if a string is palindrome or not
KMP Algorithm for Pattern Searching
Different methods to reverse a string in C/C++
Array of Strings in C++ (5 Different Ways to Create)
Convert string to char array in C++
Longest Palindromic Substring | Set 1
Caesar Cipher in Cryptography
Check whether two strings are anagram of each other
Top 50 String Coding Problems for Interviews