Given a sentence having lowercase characters, the task is to convert it to Camel Case. In Camel Case, words are joined without spaces, the first word keeps its original case, and each subsequent word starts with an uppercase letter.
Examples:
Input: "i got intern at geeksforgeeks"
Output: "iGotInternAtGeeksforgeeks"Input: "here comes the garden"
Output: "hereComesTheGarden"
Approach:
The idea is to traverse the sentence and each time a space is encountered, remove it and capitalize the next character. We use a flag, capitalizeNext, to track when to capitalize the next character. When a space is encountered, we skip it and set capitalizeNext to true. Once a character is found, we capitalize it and set capitalizeNext back to false.
// C++ program to convert given sentence to camel case
#include <iostream>
using namespace std;
string convertToCamelCase(string &s) {
string res = "";
// Flag to indicate when to capitalize the next letter
bool capitalizeNext = false;
for(int i = 0; i < s.length(); i++) {
// If we encounter a space, set the flag to capitalize
// the next character
if (s[i] == ' ') {
capitalizeNext = true;
}
// If the flag is set, capitalize the current character
else if (capitalizeNext == true) {
res += toupper(s[i]);
// Reset the flag after capitalization
capitalizeNext = false;
}
// Otherwise, just add the current character to the result
else {
res += s[i];
}
}
return res;
}
int main() {
string s = "i got intern at geeksforgeeks";
cout << convertToCamelCase(s);
return 0;
}
// Java program to convert given sentence to camel case
class GfG {
static String convertToCamelCase(String s) {
StringBuilder res = new StringBuilder();
// Flag to indicate when to capitalize the next letter
boolean capitalizeNext = false;
for (int i = 0; i < s.length(); i++) {
// If we encounter a space, set the flag to capitalize
// the next character
if (s.charAt(i) == ' ') {
capitalizeNext = true;
}
// If the flag is set, capitalize the current character
else if (capitalizeNext == true) {
res.append(Character.toUpperCase(s.charAt(i)));
// Reset the flag after capitalization
capitalizeNext = false;
}
// Otherwise, just add the current character to the result
else {
res.append(s.charAt(i));
}
}
return res.toString();
}
public static void main(String[] args) {
String s = "i got intern at geeksforgeeks";
System.out.println(convertToCamelCase(s));
}
}
# Python program to convert given sentence to camel case
def convertToCamelCase(s):
res = []
# Flag to indicate when to capitalize the next letter
capitalizeNext = False
for i in range(len(s)):
# If we encounter a space, set the flag to capitalize
# the next character
if s[i] == ' ':
capitalizeNext = True
# If the flag is set, capitalize the current character
elif capitalizeNext:
res.append(s[i].upper())
# Reset the flag after capitalization
capitalizeNext = False
# Otherwise, just add the current character to the result
else:
res.append(s[i])
return ''.join(res)
if __name__ == "__main__":
s = "i got intern at geeksforgeeks"
print(convertToCamelCase(s))
// C# program to convert given sentence to camel case
using System;
using System.Text;
class GfG {
static string convertToCamelCase(string s) {
StringBuilder res = new StringBuilder();
// Flag to indicate when to capitalize the next letter
bool capitalizeNext = false;
for (int i = 0; i < s.Length; i++) {
// If we encounter a space, set the flag to capitalize
// the next character
if (s[i] == ' ') {
capitalizeNext = true;
}
// If the flag is set, capitalize the current character
else if (capitalizeNext == true) {
res.Append(Char.ToUpper(s[i]));
// Reset the flag after capitalization
capitalizeNext = false;
}
// Otherwise, just add the current character to result
else {
res.Append(s[i]);
}
}
return res.ToString();
}
static void Main() {
string s = "i got intern at geeksforgeeks";
Console.WriteLine(convertToCamelCase(s));
}
}
// JavaScript program to convert given sentence to camel case
function convertToCamelCase(s) {
let res = [];
// Flag to indicate when to capitalize the next letter
let capitalizeNext = false;
for (let i = 0; i < s.length; i++) {
// If we encounter a space, set the flag to capitalize
// the next character
if (s[i] === ' ') {
capitalizeNext = true;
}
// If the flag is set, capitalize the current character
else if (capitalizeNext === true) {
res.push(s[i].toUpperCase());
// Reset the flag after capitalization
capitalizeNext = false;
}
// Otherwise, just add the current character to the result
else {
res.push(s[i]);
}
}
return res.join('');
}
// Driver Code
let s = "i got intern at geeksforgeeks";
console.log(convertToCamelCase(s));
Output
iGotInternAtGeeksforgeeks
Time Complexity: O(n), where n is the length of input sentence.
Auxiliary Space: O(n), to store the final result.