
- Data Structures & Algorithms
- DSA - Home
- DSA - Overview
- DSA - Environment Setup
- Algorithm
- DSA - Algorithms Basics
- DSA - Asymptotic Analysis
- DSA - Greedy Algorithms
- DSA - Divide and Conquer
- DSA - Dynamic Programming
- Data Structures
- DSA - Data Structure Basics
- DSA - Data Structures and Types
- DSA - Array Data Structure
- Linked Lists
- DSA - Linked List Basics
- DSA - Doubly Linked List
- DSA - Circular Linked List
- Stack & Queue
- DSA - Stack
- DSA - Expression Parsing
- DSA - Queue
- Searching Techniques
- DSA - Linear Search
- DSA - Binary Search
- DSA - Interpolation Search
- DSA - Hash Table
- Sorting Techniques
- DSA - Sorting Algorithms
- DSA - Bubble Sort
- DSA - Insertion Sort
- DSA - Selection Sort
- DSA - Merge Sort
- DSA - Shell Sort
- DSA - Quick Sort
- Graph Data Structure
- DSA - Graph Data Structure
- DSA - Depth First Traversal
- DSA - Breadth First Traversal
- Tree Data Structure
- DSA - Tree Data Structure
- DSA - Tree Traversal
- DSA - Binary Search Tree
- DSA - AVL Tree
- DSA - Red Black Trees
- DSA - B Trees
- DSA - B+ Trees
- DSA - Splay Trees
- DSA - Spanning Tree
- DSA - Tries
- DSA - Heap
- Recursion
- DSA - Recursion Basics
- DSA - Tower of Hanoi
- DSA - Fibonacci Series
- DSA Useful Resources
- DSA - Questions and Answers
- DSA - Quick Guide
- DSA - Useful Resources
- DSA - Discussion
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.
- Related Articles
- How to convert a string to camel case in JavaScript?
- Converting any string into camel case with JavaScript removing whitespace as well
- Title Case A Sentence JavaScript
- Write a program in Python to verify camel case string from the user, split camel cases, and store them in a new series
- Write down a python function to convert camel case to snake case?
- How to convert hyphens to camel case in JavaScript?
- How to Title Case a sentence in JavaScript?
- Return element-wise title cased version of string or Unicode in Numpy
- C# program to count upper and lower case characters in a given string
- Java program to count upper and lower case characters in a given string
- How to match a character from given string including case using Java regex?
- Find the word from a given sentence having given word as prefix
- Changing the case of a string using JavaScript
- Java program to count the number of consonants in a given sentence
- Java program to count the number of vowels in a given sentence
