Given a string s containing multiple words concatenated together, where each new word starts with an uppercase letter, insert spaces between the words and convert all characters to lowercase.
Examples:
Input: s = "geeksForGeeks"
Output: "geeks for geeks"
Explanation: The words in the string are "geeks", "For", "Geeks". After inserting spaces before each word and converting all characters to lowercase, the resulting sentence is "geeks for geeks".Input: s = "You"
Output: "you"
Explanation: The only word in the string is "You". After inserting spaces before each word and converting all characters to lowercase, the resulting sentence is "you".
Single Traversal - O(n) Time and O(n) Space
Traverse the string once and build the result directly. Whenever an uppercase character is found, add a space before it unless it is the first character, then convert it to lowercase.
#include <iostream>
#include <vector>
using namespace std;
string amendSentence(string s) {
vector<string> words;
string word = "";
for (int i = 0; i < s.size(); i++) {
if (i > 0 && s[i] >= 'A' && s[i] <= 'Z') {
words.push_back(word);
word = "";
}
if (s[i] >= 'A' && s[i] <= 'Z') {
word += (s[i] - 'A' + 'a');
} else {
word += s[i];
}
}
words.push_back(word);
string res = words[0];
for (int i = 1; i < words.size(); i++) {
res += " " + words[i];
}
return res;
}
int main() {
string s = "geeksForGeeks";
cout << amendSentence(s) << endl;
s = "You";
cout << amendSentence(s) << endl;
return 0;
}
import java.util.*;
class GFG {
static String amendSentence(String s) {
ArrayList<String> words = new ArrayList<>();
StringBuilder word = new StringBuilder();
for (int i = 0; i < s.length(); i++) {
if (i > 0 && s.charAt(i) >= 'A' && s.charAt(i) <= 'Z') {
words.add(word.toString());
word = new StringBuilder();
}
if (s.charAt(i) >= 'A' && s.charAt(i) <= 'Z') {
word.append((char)(s.charAt(i) - 'A' + 'a'));
} else {
word.append(s.charAt(i));
}
}
words.add(word.toString());
return String.join(" ", words);
}
public static void main(String[] args) {
String s = "geeksForGeeks";
System.out.println(amendSentence(s));
s = "You";
System.out.println(amendSentence(s));
}
}
def amendSentence(s):
words = []
word = []
for i in range(len(s)):
if i > 0 and 'A' <= s[i] <= 'Z':
words.append(''.join(word))
word = []
if 'A' <= s[i] <= 'Z':
word.append(chr(ord(s[i]) - ord('A') + ord('a')))
else:
word.append(s[i])
words.append(''.join(word))
return ' '.join(words)
if __name__ == "__main__":
s = "geeksForGeeks"
print(amendSentence(s))
s = "You"
print(amendSentence(s))
using System;
using System.Collections.Generic;
using System.Text;
class GFG {
static string amendSentence(string s) {
List<string> words = new List<string>();
StringBuilder word = new StringBuilder();
for (int i = 0; i < s.Length; i++) {
if (i > 0 && s[i] >= 'A' && s[i] <= 'Z') {
words.Add(word.ToString());
word = new StringBuilder();
}
if (s[i] >= 'A' && s[i] <= 'Z') {
word.Append((char)(s[i] - 'A' + 'a'));
} else {
word.Append(s[i]);
}
}
words.Add(word.ToString());
return string.Join(" ", words);
}
static void Main() {
string s = "geeksForGeeks";
Console.WriteLine(amendSentence(s));
s = "You";
Console.WriteLine(amendSentence(s));
}
}
function amendSentence(s) {
let words = [];
let word = "";
for (let i = 0; i < s.length; i++) {
if (i > 0 && s[i] >= 'A' && s[i] <= 'Z') {
words.push(word);
word = "";
}
if (s[i] >= 'A' && s[i] <= 'Z') {
word += String.fromCharCode(
s.charCodeAt(i) - 'A'.charCodeAt(0) + 'a'.charCodeAt(0)
);
} else {
word += s[i];
}
}
words.push(word);
return words.join(" ");
}
// Driver Code
let s = "geeksForGeeks";
console.log(amendSentence(s));
s = "You";
console.log(amendSentence(s));
Output
geeks for geeks you