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++ 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 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 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# 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
Time complexity: O(n)
Auxiliary Space: O(1)
Put spaces between words starting with capital letters using Regex.
// C++ Program to solve the above problem
#include <bits/stdc++.h>
using namespace std;
// Function to insert spaces between words
void putSpace(string input) {
// Define a regex pattern to match words
// starting with a capital letter
regex pattern("[A-Z][a-z]*");
smatch match;
vector<string> words;
// Find all matches using regex
while (regex_search(input, match, pattern)) {
words.push_back(match.str());
input = match.suffix();
}
// Change the first letter of each word to lowercase
for (int i = 0; i < words.size(); i++) {
words[i][0] = tolower(words[i][0]);
}
// Print the words with spaces
for (int i = 0; i < words.size(); i++) {
cout << words[i] << " ";
}
cout << endl;
}
// Driver program
int main() {
string input = "BruceWayneIsBatman";
putSpace(input);
return 0;
}
// THIS CODE IS CONTRIBUTED BY KIRTI AGARWAL
// Java code for the above approach
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
class GFG {
// Function to insert spaces between words
public static void putSpace(String input) {
// Define a regex pattern to match words starting with a capital letter
Pattern pattern = Pattern.compile("[A-Z][a-z]*");
Matcher matcher = pattern.matcher(input);
List<String> words = new ArrayList<>();
// Find all matches using regex
while (matcher.find()) {
words.add(matcher.group());
}
// Change the first letter of each word to lowercase
for (int i = 0; i < words.size(); i++) {
String word = words.get(i);
words.set(i, word.substring(0, 1).toLowerCase() + word.substring(1));
}
// Print the words with spaces
for (String word : words) {
System.out.print(word + " ");
}
System.out.println();
}
// Driver program
public static void main(String[] args) {
String input = "BruceWayneIsBatman";
putSpace(input);
}
}
// This code is contributed by Kirti Agarwal
import re
def putSpace(input):
# regex [A-Z][a-z]* means any string starting
# with capital character followed by many
# lowercase letters
words = re.findall('[A-Z][a-z]*', input)
# Change first letter of each word into lower
# case
for i in range(0, len(words)):
words[i] = words[i][0].lower()+words[i][1:]
print(' '.join(words))
# Driver program
if __name__ == "__main__":
input = 'BruceWayneIsBatman'
putSpace(input)
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
public class GFG
{
// Function to insert spaces between words
// Define a regex pattern to match words starting with a capital letter
public static void PutSpace(string input)
{
Regex pattern = new Regex("[A-Z][a-z]*");
Match match;
List<string> words = new List<string>();
// Find all matches using regex
while ((match = pattern.Match(input)).Success)
{
words.Add(match.Value);
input = input.Substring(match.Index + match.Length);
}
// Change the first letter of each word to lowercase
for (int i = 0; i < words.Count; i++)
{
char[] wordChars = words[i].ToCharArray();
wordChars[0] = char.ToLower(wordChars[0]);
words[i] = new string(wordChars);
}
// Print the words with spaces
foreach (string word in words)
{
Console.Write(word + " ");
}
Console.WriteLine();
}
// Driver program
public static void Main(string[] args)
{
string input = "BruceWayneIsBatman";
PutSpace(input);
}
}
function putSpace(input) {
// regex [A-Z][a-z]* means any string starting
// with capital character followed by many
// lowercase letters
const regex = new RegExp('[A-Z][a-z]*', 'g');
const words = input.match(regex);
// Change first letter of each word into lower
// case
for (let i = 0; i < words.length; i++) {
words[i] = words[i][0].toLowerCase() + words[i].substring(1);
}
console.log(words.join(' '));
}
// Driver program
const input = 'BruceWayneIsBatman';
putSpace(input);
Output
bruce wayne is batman
Time complexity: O(n)
Auxiliary Space: O(n)