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++ 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; } |
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
<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
Time complexity: O(n)
Auxiliary Space: O(1)
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.