Sentence Case of a given Camel cased string

Given a string str in camelCase format, the task is to convert the string into a readable form.

Examples:

Input: str = “ILoveGeeksForGeeks”
Output: I love geeks for geeks



Input: str = “WeLoveToCode”
Output: We love to code

Approach: The string is camelCased that means words are not separated by spaces instead every capital letter means a new word has begun. In order to improve its readability, we convert it into sentence case. Given below is the code for the conversion. We traverse through the string and then whenever we encounter a lowercase we print it as such. When we encounter an uppercase letter we output space followed by the uppercase character (after converting to lowercase) and then the rest of the characters.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to return the original string
// after converting it back from camelCase
void getOrgString(string s)
{
  
    // Print the first character as it is
    cout << s[0];
  
    // Traverse the rest of the
    // characters one by one
    int i = 1;
    while (i < s.length()) {
  
        // If current character is uppercase
        // print space followed by the
        // current character in lowercase
        if (s[i] >= 'A' && s[i] <= 'Z')
            cout << " " << (char)tolower(s[i]);
  
        // Else print the current character
        else
            cout << s[i];
        i++;
    }
}
  
// Driver code
int main()
{
    string s = "ILoveGeeksForGeeks";
  
    getOrgString(s);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach 
class GFG 
{
  
    // Function to return the original string 
    // after converting it back from camelCase 
    static void getOrgString(String s) 
    
      
        // Print the first character as it is 
        System.out.print(s.charAt(0)); 
      
        // Traverse the rest of the 
        // characters one by one 
        int i = 1
        while (i < s.length()) 
        
      
            // If current character is uppercase 
            // print space followed by the 
            // current character in lowercase 
            if (s.charAt(i) >= 'A' && s.charAt(i) <= 'Z'
                System.out.print(" "+ Character.toLowerCase(s.charAt(i))); 
      
            // Else print the current character 
            else
                System.out.print(s.charAt(i)); 
                  
            i++; 
        
    
      
    // Driver code 
    public static void main (String[] args) 
    {
                String s = "ILoveGeeksForGeeks"
                getOrgString(s); 
    }
}
  
// This code is contributed by AnkitRai01

chevron_right


Python

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the approach
  
# Function to return the original string
# after converting it back from camelCase
def getOrgString(s):
  
  
    # Print the first character as it is
    print(s[0],end="")
  
    # Traverse the rest of the
    # characters one by one
    i = 1
    while (i < len(s)):
  
        # If current character is uppercase
        # prspace followed by the
        # current character in lowercase
        if (ord(s[i]) >= ord('A') and ord(s[i] )<= ord('Z')):
            print(" ",s[i].lower(),end="")
  
        # Else print the current character
        else:
            print(s[i],end="")
        i+=1
  
# Driver code
  
s = "ILoveGeeksForGeeks"
  
getOrgString(s)
  
# This code is contributed by mohit kumar 29

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the approach
using System;
      
class GFG 
{
  
    // Function to return the original string 
    // after converting it back from camelCase 
    static void getOrgString(String s) 
    
      
        // Print the first character as it is 
        Console.Write(s[0]); 
      
        // Traverse the rest of the 
        // characters one by one 
        int i = 1; 
        while (i < s.Length) 
        
      
            // If current character is uppercase 
            // print space followed by the 
            // current character in lowercase 
            if (s[i] >= 'A' && s[i] <= 'Z'
                Console.Write(" "+ char.ToLower(s[i])); 
      
            // Else print the current character 
            else
                Console.Write(s[i]); 
                  
            i++; 
        
    
      
    // Driver code 
    public static void Main (String[] args) 
    {
                String s = "ILoveGeeksForGeeks"
                getOrgString(s); 
    }
}
  
/* This code is contributed by PrinciRaj1992 */

chevron_right


Output:

I love geeks for geeks


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.





Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.