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:
- Put a single space between these words.
- 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.
Implementation:
C++
#include <iostream>
using namespace std;
void amendSentence(string str)
{
for ( int i=0; i < str.length(); i++)
{
if (str[i]>= 'A' && str[i]<= 'Z' )
{
str[i]=str[i]+32;
if (i != 0)
cout << " " ;
cout << str[i];
}
else
cout << str[i];
}
}
int main()
{
string str = "BruceWayneIsBatman" ;
amendSentence(str);
return 0;
}
|
Java
import java.util.*;
import java.lang.*;
import java.io.*;
class AddSpaceinSentence
{
public static void amendSentence(String sstr)
{
char [] str=sstr.toCharArray();
for ( int i= 0 ; i < str.length; i++)
{
if (str[i]>= 'A' && str[i]<= 'Z' )
{
str[i] = ( char )(str[i]+ 32 );
if (i != 0 )
System.out.print( " " );
System.out.print(str[i]);
}
else
System.out.print(str[i]);
}
}
public static void main (String[] args)
{
String str = "BruceWayneIsBatman" ;
amendSentence(str);
}
}
|
Python3
def amendSentence(string):
string = list (string)
for i in range ( len (string)):
if string[i] > = 'A' and string[i] < = 'Z' :
string[i] = chr ( ord (string[i]) + 32 )
if i ! = 0 :
print ( " " , end = "")
print (string[i], end = "")
else :
print (string[i], end = "")
if __name__ = = "__main__" :
string = "BruceWayneIsBatman"
amendSentence(string)
|
C#
using System;
public class GFG {
public static void amendSentence( string sstr)
{
char [] str = sstr.ToCharArray();
for ( int i = 0; i < str.Length; i++)
{
if (str[i] >= 'A' && str[i] <= 'Z' )
{
str[i] = ( char )(str[i] + 32);
if (i != 0)
Console.Write( " " );
Console.Write(str[i]);
}
else
Console.Write(str[i]);
}
}
public static void Main ()
{
string str = "BruceWayneIsBatman" ;
amendSentence(str);
}
}
|
Javascript
<script>
function amendSentence(sstr)
{
let str = sstr.split( '' );
for (let i = 0; i < str.length; i++)
{
if (str[i].charCodeAt() >= 'A' .charCodeAt() &&
str[i].charCodeAt() <= 'Z' .charCodeAt())
{
str[i] =
String.fromCharCode(str[i].charCodeAt() + 32);
if (i != 0)
document.write( " " );
document.write(str[i]);
}
else
document.write(str[i]);
}
}
let str = "BruceWayneIsBatman" ;
amendSentence(str);
</script>
|
Output
bruce wayne is batman
Time complexity: O(n)
Auxiliary Space: O(1)
Put spaces between words starting with capital letters using Regex.
C++
#include <bits/stdc++.h>
using namespace std;
void putSpace(string input) {
regex pattern( "[A-Z][a-z]*" );
smatch match;
vector<string> words;
while (regex_search(input, match, pattern)) {
words.push_back(match.str());
input = match.suffix();
}
for ( int i = 0; i < words.size(); i++) {
words[i][0] = tolower (words[i][0]);
}
for ( int i = 0; i < words.size(); i++) {
cout << words[i] << " " ;
}
cout << endl;
}
int main() {
string input = "BruceWayneIsBatman" ;
putSpace(input);
return 0;
}
|
Java
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
class GFG {
public static void putSpace(String input) {
Pattern pattern = Pattern.compile( "[A-Z][a-z]*" );
Matcher matcher = pattern.matcher(input);
List<String> words = new ArrayList<>();
while (matcher.find()) {
words.add(matcher.group());
}
for ( int i = 0 ; i < words.size(); i++) {
String word = words.get(i);
words.set(i, word.substring( 0 , 1 ).toLowerCase() + word.substring( 1 ));
}
for (String word : words) {
System.out.print(word + " " );
}
System.out.println();
}
public static void main(String[] args) {
String input = "BruceWayneIsBatman" ;
putSpace(input);
}
}
|
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 )
|
C#
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
public class GFG
{
public static void PutSpace( string input)
{
Regex pattern = new Regex( "[A-Z][a-z]*" );
Match match;
List< string > words = new List< string >();
while ((match = pattern.Match(input)).Success)
{
words.Add(match.Value);
input = input.Substring(match.Index + match.Length);
}
for ( int i = 0; i < words.Count; i++)
{
char [] wordChars = words[i].ToCharArray();
wordChars[0] = char .ToLower(wordChars[0]);
words[i] = new string (wordChars);
}
foreach ( string word in words)
{
Console.Write(word + " " );
}
Console.WriteLine();
}
public static void Main( string [] args)
{
string input = "BruceWayneIsBatman" ;
PutSpace(input);
}
}
|
Javascript
function putSpace(input) {
const regex = new RegExp( '[A-Z][a-z]*' , 'g' );
const words = input.match(regex);
for (let i = 0; i < words.length; i++) {
words[i] = words[i][0].toLowerCase() + words[i].substring(1);
}
console.log(words.join( ' ' ));
}
const input = 'BruceWayneIsBatman' ;
putSpace(input);
|
Output
bruce wayne is batman
Time complexity: O(n)
Auxiliary Space: O(n)
Last Updated :
15 Sep, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...