Put spaces between words starting with capital letters

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/C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to put spaces between words starting
// with capital letters.
#include <iostream>
using namespace std;
  
// Function to amend the sentence
void 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 code
int main()
{
    string str ="BruceWayneIsBatman";
    amendSentence(str);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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.

chevron_right



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 contribute.geeksforgeeks.org or mail your article to contribute@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.



My Personal Notes arrow_drop_up

Improved By : Sam007



Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.