📌  相关文章
📜  在以大写字母开头的单词之间放置空格

📅  最后修改于: 2022-05-13 01:57:08.677000             🧑  作者: Mango

在以大写字母开头的单词之间放置空格

你得到一个字符数组,基本上是一个句子。但是,不同的单词之间没有空格,并且每个单词的第一个字母都是大写的。您需要在以下修改后打印这句话:
(i) 在这些词之间放置一个空格。
(ii) 将大写字母转换为小写字母。

例子:

Input : BruceWayneIsBatman
Output : bruce wayne is batman

Input :  You
Output :  you

我们检查当前字符是否为大写,然后打印“”(空格)并将其转换为小写。

C++
// C++ program to put spaces between words starting
// with capital letters.
#include 
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;
}


Java
// 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
# Python3 program to put spaces between words
# starting with capital letters.
 
# Function to amend the sentence
def 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 Code
if __name__ == "__main__":
    string = "BruceWayneIsBatman"
    amendSentence(string)
 
# This code is contributed by
# sanjeev2552


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


Javascript


输出:

bruce wayne is batman