Camel case of a given sentence

Given a sentence, task is to remove spaces from the sentence and rewrite in Camel case. It is a style of writing where we don’t have spaces and all words begin with capital letters.

Examples:

Input  : I got intern at techcodebit
Output : IGotInternAtTECHCODEBIT

Input :  Here comes the garden
Output : HereComesTheGarden

Simple solution: First method is to traverse sentence and one by one remove spaces by moving subsequent characters one position back and changing case of first character to capital. It takes O(n*n) time.

Efficient solution : We traverse given string, while traversing we copy non space character to result and whenever we encounter space, we ignore it and change next letter to capital.

Below is c++ code implementation

// CPP program to convert given sentence
/// to camel case.
#include <bits/stdc++.h>
using namespace std;
// Function to remove spaces and convert
// into camel case
string convert(string s)
{
    int n = s.length();
    int res_ind = 0;
    for (int i = 0; i < n; i++) {
        // check for spaces in the sentence
        if (s[i] == ' ') {
            // conversion into upper case
            s[i + 1] = toupper(s[i + 1]);
            continue;
        }
        // If not space, copy character
        else
            s[res_ind++] = s[i];       
    }
    s[res_ind] = '\0';
    // return string to main
    return s;
}
// Driver program
int main()
{
    string str = "I get intern at <a href="#">techcodebit</a>";
    cout << convert(str);
    return 0;
}

Output:

IGetInternAtTECHCODEBIT

Disclaimer: This does not belong to TechCodeBit, its an article taken from the below
source and credits.
source and credits:http://www.geeksforgeeks.org/camel-case-given-sentence/
We have built the accelerating growth-oriented website for budding engineers and aspiring job holders of technology companies such as Google, Facebook, and Amazon
If you would like to study our free courses you can join us at

http://www.techcodebit.com. #techcodebit #google #microsoft #facebook #interview portal #jobplacements
#technicalguide

rakesh

Leave a Reply

Your email address will not be published. Required fields are marked *

Skip to toolbar