Sentence Case of a given Camel cased String


A C++ string is a collection of characters forming words. It may contains letters, numbers and even special characters. The sentences of a string can be clubbed together in different ways to form different types of representations.

The camel case of a string denotes the string in such a way that the following two properties are maintained −

  • The words are joined together, there is no space character.

  • Every word has the first letter stored in upper case.

Thereby, the upper case letters in this form of representation can be used to segregate the different words. This type of representation is not easily readable, yet it is widely used in programming domain.

Another type of representation of the string is the sentence case, wherein, the words are separated by a space character and all the words except the first begin with a lowercase letter.

In the following problem, the camel case of a given string has to be converted into the sentence case representation.

Some of the examples illustrating the problem statement are as follows −

Sample Example

Example 1 - str : IdentifyThe@abc

Output : Identify the@abc

Explanation: Special characters are also printed as it is

Example 2 - str : ThisIsCamelCase

Output : This is camel case

Explanation: The first letter is printed as it is during the output.

This problem can be solved by character case checking and then its conversion to the opposite case, if required.

Algorithm

  • Step 1 − A for loop is used to iterate through the input string provided.

  • Step 2 − In case, the pointer is at the first character, it is printed as it is.

  • Step 3 − For the rest of the characters, if an upper case letter is found, a space character is first displayed. Then the letter is converted to lower case and displayed.

  • Step 4 − Else if, any lowercase character is printed as it is. Step 5 - Else, any special character is printed as it is.

Example

The following code snippet takes as example a camel cased C++ string and converts it to sentence case respectively −

//including the required libraries
#include <bits/stdc++.h>
using namespace std;

//convert camelcase string to sentence case respectively
void sentenceCase(string str){

   //getting the length of string
   int len = str.length();

   //iterating over the string
   for(int i=0;i<len;i++) {

      //printing the first character of the string
      if(i==0){
         cout << str[0];
      } else {

         //check if current character is in upper case convert to lower case and insert a space before it to separate the words
         if (str[i] >= 'A' && str[i] <= 'Z'){

            //printing a space before character
            cout << " " ;
            char ch = (char)tolower(str[i]);

            //printing the character in lower case
            cout << ch;
         }

         //if character already in lower case print as it is
         else
            cout << str[i];
      }
   }
}
//calling the method
int main(){

   //sample string
   string s = "ConvertCamelCaseToSentenceCase";
   cout<<"Entered String :"<<s;
   cout<<"\nConverted String:";

   //print the sentence case
   sentenceCase(s);
   return 0;
}

Output

Entered String :ConvertCamelCaseToSentenceCase
Converted String:Convert camel case to sentence case

Conclusion

Case conversion can be easily performed in case of strings. Sentence case of a string enhances the readability. It makes the words easily understandable by separating them with a space. The time complexity of the above specified approach is O(n), in worst case, where n is the length of the string. Therefore, the algorithm works in linear time. The space complexity of the above specified algorithm is O(1), which is constant in nature.

Updated on: 15-Mar-2023

456 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements