Given a string in Camel Case format, we need to extract all the words which are present in the string.
CamelCase is the sequence of one or more than one words having the following properties:
- It is a concatenation of one or more words consisting of English letters.
- All letters in the first word are lowercase.
- For each of the subsequent words, the first letter is uppercase and the rest of the letters are lowercase.
Example:
Input: str = “GeeksForGeeks”
Output: Geeks For Geeks
Input: str = “AComputerSciencePortalForGeeks”
Output: A Computer Science Portal For Geeks
Approach: A simple approach is to traverse the array and extract every word by looking at its first character, as it will always be in upper-case. Store all extracted words in a new array and print them.
C++
#include <cstring>
#include <iostream>
using namespace std;
char * mystrtok( char * str)
{
static char * input = NULL;
if (str != NULL) {
input = str;
}
if (input == NULL)
return NULL;
char * output = new char [ strlen (input + 1)];
int i = 0;
output[i] = input[i];
i++;
for (; input[i] != '\0' ; i++) {
if (! isupper (input[i]))
output[i] = input[i];
else {
output[i] = '\0' ;
input = input + i;
return output;
}
}
output[i] = '\0' ;
input = NULL;
return output;
}
void extractWords( char * s)
{
char * ptr = mystrtok(s);
cout << ptr << endl;
while (ptr != NULL) {
ptr = mystrtok(NULL);
cout << ptr << endl;
}
}
int main()
{
char s[] = "GeeksForGeeks" ;
extractWords(s);
return 0;
}
|
Java
class Main {
public static String mystrtok(String str, String[] input) {
if (str != null ) {
input[ 0 ] = str;
}
if (input[ 0 ] == null )
return null ;
String output = "" ;
int i = 0 ;
output += input[ 0 ].charAt(i);
i++;
for (; i < input[ 0 ].length(); i++) {
if (!Character.isUpperCase(input[ 0 ].charAt(i)))
output += input[ 0 ].charAt(i);
else {
output += '\0' ;
input[ 0 ] = input[ 0 ].substring(i);
return output;
}
}
input[ 0 ] = null ;
return output;
}
public static void extractWords(String s) {
String[] input = new String[ 1 ];
String ptr = mystrtok(s, input);
System.out.println(ptr);
while (ptr != null ) {
ptr = mystrtok( null , input);
if (ptr != null )
System.out.println(ptr);
}
}
public static void main(String[] args) {
String s = "GeeksForGeeks" ;
extractWords(s);
}
}
|
Python
def my_strtok(s, input ):
if s is not None :
input [ 0 ] = s
if input [ 0 ] is None :
return None
output = ""
i = 0
output + = input [ 0 ][i]
i + = 1
while i < len ( input [ 0 ]):
if not input [ 0 ][i].isupper():
output + = input [ 0 ][i]
else :
output + = '\0'
input [ 0 ] = input [ 0 ][i:]
return output
i + = 1
input [ 0 ] = None
return output
def extract_words(s):
input = [ None ]
ptr = my_strtok(s, input )
print (ptr)
while ptr is not None :
ptr = my_strtok( None , input )
if ptr is not None :
print (ptr)
if __name__ = = "__main__" :
s = "GeeksForGeeks"
extract_words(s)
|
Javascript
function myStrtok(s, input) {
if (s !== null ) {
input[0] = s;
}
if (input[0] === null ) {
return null ;
}
let output = "" ;
let i = 0;
output += input[0][i];
i++;
while (i < input[0].length) {
if (!input[0][i].match(/[A-Z]/)) {
output += input[0][i];
} else {
output += '\0' ;
input[0] = input[0].substring(i);
return output;
}
i++;
}
input[0] = null ;
return output;
}
function extractWords(s) {
let input = [ null ];
let ptr = myStrtok(s, input);
console.log(ptr);
while (ptr !== null ) {
ptr = myStrtok( null , input);
if (ptr !== null ) {
console.log(ptr);
}
}
}
let s = "GeeksForGeeks" ;
extractWords(s);
|
Time Complexity: O(N)
Auxiliary Space: O(N), as we needed a new array to store the output.
Using Regular Expression:
C++
#include <iostream>
#include <regex>
using namespace std;
int main() {
string str = "GeeksForGeeks" ;
regex camelCaseRegex( "([a-z])([A-Z])" );
string result = regex_replace(str, camelCaseRegex, "$1 $2" );
for ( char c : result) {
if (c != ' ' )
cout << c;
else
cout << "\n" ;
}
return 0;
}
|
Java
import java.io.*;
class GFG {
public static void main(String[] args)
{
String str = "GeeksForGeeks" ;
for (
String w : str.split(
"(?<!(^|[A-Z]))(?=[A-Z])|(?<!^)(?=[A-Z][a-z])" )) {
System.out.println(w);
}
}
}
|
Python
import re
def split_camel_case(identifier):
matches = re.finditer( '.+?(?:(?<=[a-z])(?=[A-Z])|(?<=[A-Z])(?=[A-Z][a-z])|$)' , identifier)
return [m.group( 0 ) for m in matches]
print (split_camel_case( "GeeksForGeeks" ))
|
C#
using System;
public class GFG {
public static void Main( string [] args) {
string str = "GeeksForGeeks" ;
foreach ( string w in System.Text.RegularExpressions.Regex.Split(str, "(?<!(^|[A-Z]))(?=[A-Z])|(?<!^)(?=[A-Z][a-z])" )) {
Console.WriteLine(w);
}
}
}
|
Javascript
function splitCamelCase(identifier) {
return identifier.match(/.+?(?:(?<=[a-z])(?=[A-Z])|(?<=[A-Z])(?=[A-Z][a-z])|$)/g);
}
console.log(splitCamelCase( "GeeksForGeeks" ));
|
Output
Geeks
For
Geeks
Time Complexity: O(N)
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
27 Nov, 2023
Like Article
Save Article