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++
#include <bits/stdc++.h>
using namespace std;
void getOrgString(string s)
{
cout << s[0];
int i = 1;
while (i < s.length()) {
if (s[i] >= 'A' && s[i] <= 'Z' )
cout << " " << ( char ) tolower (s[i]);
else
cout << s[i];
i++;
}
}
int main()
{
string s = "ILoveGeeksForGeeks" ;
getOrgString(s);
return 0;
}
|
Java
class GFG
{
static void getOrgString(String s)
{
System.out.print(s.charAt( 0 ));
int i = 1 ;
while (i < s.length())
{
if (s.charAt(i) >= 'A' && s.charAt(i) <= 'Z' )
System.out.print( " " + Character.toLowerCase(s.charAt(i)));
else
System.out.print(s.charAt(i));
i++;
}
}
public static void main (String[] args)
{
String s = "ILoveGeeksForGeeks" ;
getOrgString(s);
}
}
|
Python
def getOrgString(s):
print (s[ 0 ],end = "")
i = 1
while (i < len (s)):
if ( ord (s[i]) > = ord ( 'A' ) and ord (s[i] )< = ord ( 'Z' )):
print ( " " ,s[i].lower(),end = "")
else :
print (s[i],end = "")
i + = 1
s = "ILoveGeeksForGeeks"
getOrgString(s)
|
C#
using System;
class GFG
{
static void getOrgString(String s)
{
Console.Write(s[0]);
int i = 1;
while (i < s.Length)
{
if (s[i] >= 'A' && s[i] <= 'Z' )
Console.Write( " " + char .ToLower(s[i]));
else
Console.Write(s[i]);
i++;
}
}
public static void Main (String[] args)
{
String s = "ILoveGeeksForGeeks" ;
getOrgString(s);
}
}
|
Javascript
<script>
function getOrgString(s)
{
document.write(s[0]);
var i = 1;
while (i < s.length)
{
if (
s[i].charCodeAt(0) >= "A" .charCodeAt(0) &&
s[i].charCodeAt(0) <= "Z" .charCodeAt(0)
)
document.write( " " + s[i].toLowerCase());
else document.write(s[i]);
i++;
}
}
var s = "ILoveGeeksForGeeks" ;
getOrgString(s);
</script>
|
Output:
I love geeks for geeks
Time Complexity: O(n)
Auxiliary Space: O(1)
Last Updated :
23 Aug, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...