Camel Case in Java
Java names its classes, interfaces, methods, as well as variables using camel-case syntax. If the name consists of two words, the second word will always begin with a capital letter, such as maxMarks(), lastName, or ClassTest, and all whitespace will be removed.
Camel case can be used in two different ways:
- Use lowercase camel case, when the first letter of the initial word is lowercase. When naming the methods as well as variables, this convention is typically followed. For examples, FirstName, LastName, ActionEvent, printArray(), etc.
- The title case, also known as upper camel case, is used when the first letter of the beginning word is capitalized. This naming style is typically used for classes and interfaces. For examples, Employee, printable, etc.
Changing a Standard String to a Camel Case
Simply deleting the spaces from a string will transform it to either the lowercase or uppercase camel case style.
Example of a Lower Camel Case
Input: Learn the programming languages
Output: learnTheProgrammingLanguages
Example of an Upper Camel Case
Input: learn java language
Output: LearnJavaLanguage
Steps
Step 1: The character array is traversed character by character until the end using the following algorithm.
Step 2: When lower camel case comes next, the initial letter of a string at index = 0 is changed to lower case, otherwise, it is converted to upper case
Step 3: When there are any spaces in the array, the letter that follows the space gets capitalized.
Step 4: If the non-space characters appear, it is copied to the array that results.
Time complexity
Time complexity: O(n)
Since all operations need one traversing of the string, the total time needed by the approach is linear.
Auxiliary Space:
Auxiliary Space: O(1)
Since no additional array is utilized, the algorithm's space requirements are constant.
Programs on Camel Case in Java
Lower Camel Case Conversion of String
public class LowerCamel
{
static String convertString( String s )
{
int ctr = 0 ;
int n = s.length( ) ;
char ch[ ] = s.toCharArray( ) ;
int c = 0 ;
for ( inti = 0; i< n; i++ )
{
if( i == 0 )
ch[i ] = Character.toLowerCase( ch[ i ] ) ;
if ( ch[ i ] == ' ' )
{
ctr++ ;
ch[i + 1 ] = Character.toUpperCase( ch[ i + 1 ] ) ;
continue ;
}
else
ch[c++ ] = ch[ i ] ;
}
return String.valueOf( ch, 0, n - ctr ) ;
}
public static void main( Stringargs[ ] )
{
String str = "Hello People" ;
System.out.println( convertString( str ) ) ;
String str1 = "Learn" ;
System.out.println( convertString( str1 ) ) ;
String str2 = " The best content from the javatpointtutourial" ;
System.out.println( convertString( str2 ) ) ;
}
}
Output
helloPeople
learn
TheBestContentFromTheJavatpointTutourial
Upper Camel Case Conversion for String
public class UpperCamel
{
static String convertString( String s )
{
int ctr = 0 ;
int n = s.length( ) ;
char ch[ ] = s.toCharArray( ) ;
int c = 0 ;
for ( inti = 0; i< n; i++ )
{
if( i == 0 )
ch[i ] = Character.toUpperCase( ch[ i ] ) ;
if ( ch[ i ] == ' ' )
{
ctr++ ;
ch[i + 1 ] = Character.toUpperCase( ch[ i + 1] ) ;
continue ;
}
else
ch[c++ ] = ch[ i ] ;
}
return String.valueOf( ch, 0, n - ctr ) ;
}
public static void main( Stringargs[ ] )
{
String str = "programming language" ;
System.out.println( convertString( str ) ) ;
String str1 = "java t point" ;
System.out.println( convertString( str1 ) ) ;
String str2 = "i love java language" ;
System.out.println(convertString( str2 ) ) ;
}
}
Output
ProgrammingLanguage
JavaTPoint
ILoveJavaLanguage
Example
public class UpperCamel
{
static String convertString( String s )
{
int ctr = 0 ;
int n = s.length( ) ;
char ch[ ] = s.toCharArray( ) ;
int c = 0 ;
for ( inti = 0; i< n; i++ )
{
if( i == 0 )
ch[i ] = Character.toUpperCase( ch[ i ] ) ;
if ( ch[ i ] == ' ' )
{
ctr++ ;
ch[i + 1 ] = Character.toUpperCase( ch[ i + 1] ) ;
continue ;
}
else
ch[c++ ] = ch[ i ] ;
}
return String.valueOf( ch, 0, n - ctr ) ;
}
public static void main( Stringargs[ ] )
{
String str = " programming language" ;
System.out.println( convertString( str ) ) ;
}
}
Output
ProgrammingLanguage