Home >Backend Development >C++ >Given a camelCase named string, convert it into sentence format
A C string is a collection of words composed of characters. It can contain letters, numbers and even special characters. Sentences of strings can be combined together in different ways to form different types of representations.
Camel case nomenclature is a string representation method that keeps the following two attributes unchanged -
The words are concatenated without space characters.
The first letter of each word is stored in uppercase.
Thus, capital letters in this representation can be used to separate different words. This type of representation is not easy to read, but is widely used in programming.
Another representation of strings is sentence case, where words are separated by space characters and all but the first word begin with a lowercase letter.
In the question below, the camel case of the given string must be converted to the sentence case representation.
Some examples to illustrate the problem statement are as follows -
Example 1 - str: IdentifyThe@abc
Output: Identify @abc
Note: Special characters are also printed as is
Example 2 - str:ThisIsCamelCase
Output: This is camelCase
Description: The first letter is printed as is during the output process.
This problem can be solved by checking the character case and converting it to the opposite case if necessary.
Step 1 − Use a for loop to iterate over the provided input string.
Step 2 - If the pointer is at the first character, print as is.
Step 3 - For the remaining characters, if an uppercase letter is found, display a space character first. The letter is then converted to lowercase and displayed.
Step 4 − Otherwise, any lowercase characters are printed as is. Step 5 - Otherwise, any special characters are printed as-is.
The following code snippet takes camel case C string as an example and converts it into sentence case -
//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; }
Entered String :ConvertCamelCaseToSentenceCase Converted String:Convert camel case to sentence case
If it is a string, case conversion can be easily performed. Sentence case for strings enhances readability. You can make words easier to understand by separating them with spaces. In the worst case, the time complexity of the above specified method is O(n), 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.
The above is the detailed content of Given a camelCase named string, convert it into sentence format. For more information, please follow other related articles on the PHP Chinese website!