logo

Cas de chameau en Java

Java suit la syntaxe Camel-Case pour nommer les classes, les interfaces, les méthodes et les variables. Si le nom est combiné avec deux mots, le deuxième mot commencera toujours par une lettre majuscule, comme maxMarks(), lastName, ClassTest, en supprimant tous les espaces.

Il existe deux manières d'utiliser le boîtier Camel :

  1. Minuscule camel où le premier caractère du premier mot est en minuscule. Cette convention est généralement suivie lors de la dénomination des méthodes et des variables. Exemple, firstName, lastName, actionEvent, printArray( ), etc.
  2. La casse supérieure du chameau, également connue sous le nom de casse du titre, où le premier caractère du premier mot est en majuscule. Cette convention est généralement suivie lors de la dénomination des classes et des interfaces. Par exemple, Employé, Imprimable, etc.

Conversion d'une chaîne normale en étui camel

Une chaîne peut être convertie en convention de casse camel inférieure ou supérieure simplement en supprimant les espaces de la chaîne.

Exemple de cas de chameau inférieur :

Saisir: JavaTpoint est le meilleur site de tutoriels pour les langages de programmation.

génération de nombres aléatoires java

Sortir: javaTpointIsTheBestTutorialSiteForProgrammingLanguages.

Exemple de cas supérieur Camel :

Saisir: c'est le tutoriel Java

Sortir: ThisIsTheJavaTutorial

Algorithme:

  1. Parcourez le tableau de caractères caractère par caractère jusqu'à ce qu'il atteigne la fin.
  2. La première lettre de la chaîne à l'index = 0 est soit convertie en minuscules (lorsqu'elle suit la minuscule camel) ou en majuscule (lorsqu'elle suit la majuscule camel).
  3. Le tableau est vérifié pour les espaces et la lettre qui suit immédiatement l'espace est convertie en majuscule.
  4. Si le caractère autre qu'un espace est rencontré, il est copié dans le tableau résultant.

Implémentons l'algorithme dans un programme Java.

A. Conversion d'une chaîne en casse Camel inférieure

LowerCamel.java

 public class LowerCamel { // function to convert the string into lower camel case static String convertString( String s ) { // to keep track of spaces int ctr = 0 ; // variable to hold the length of the string int n = s.length( ) ; // converting the string expression to character array char ch[ ] = s.toCharArray( ) ; // keep track of indices of ch[ ] array int c = 0 ; // traversing through each character of the array for ( int i = 0; i <n; 0 1 i++ ) { the first position of array i.e., letter must be converted to lower case as we are following camel in this program if( i="=" converting using tolowercase( in-built function ch[ ]="Character.toLowerCase(" ; need remove all spaces between, check for empty if ( ' incrementing space counter by ctr++ immediately after upper + continue loop } is not encountered simply copy character else c++ size new string will reduced have been removed thus, returning with return string.valueof( ch, 0, n - ctr driver code public static void main( args[ passing name a method str="Max Marks()" system.out.println( convertstring( variable str1="Last name" str2="JavaTpoint is the best tutorial site for programming languages." < pre> <p> <strong>Output:</strong> </p> <pre> maxMarks() lastName javaTpointIsTheBestTutorialSiteForProgrammingLanguages. </pre> <h3>B. Converting String to Upper Camel Case</h3> <p> <strong>UpperCamel.java</strong> </p> <pre> public class UpperCamel { // function to convert the string into upper camel case static String convertString( String s ) { // to keep track of spaces int ctr = 0 ; // variable to hold the length of the string int n = s.length( ) ; // converting the string expression to character array char ch[ ] = s.toCharArray( ) ; // // keep track of indices of ch[ ] array int c = 0 ; // traversing through each character of the array for ( int i = 0; i <n; 0 1 i++ ) { the first position of array i.e., letter must be converted to upper case. we checked this before second if statement as that is executed only when it encounters space and, there no a string. if( i="=" converting case using touppercase( in-built function ch[ ]="Character.toUpperCase(" ; need remove all spaces in between, check for empty ( ' incrementing counter by ctr++ immediately after + 1] continue loop } not encountered simply copy character else c++ new string will reduced have been removed thus returning with size return string.valueof( ch, 0, n - ctr driver code public static void main( args[ passing name method str="class test" system.out.println( convertstring( variable str1="employee" str2="this is the java tutorial" system.out.println(convertstring( < pre> <p> <strong>Output:</strong> </p> <pre> ClassTest Employee ThisIsTheJavaTutorial </pre> <hr></n;></pre></n;>

B. Conversion d'une chaîne en casse Camel supérieure

UpperCamel.java

 public class UpperCamel { // function to convert the string into upper camel case static String convertString( String s ) { // to keep track of spaces int ctr = 0 ; // variable to hold the length of the string int n = s.length( ) ; // converting the string expression to character array char ch[ ] = s.toCharArray( ) ; // // keep track of indices of ch[ ] array int c = 0 ; // traversing through each character of the array for ( int i = 0; i <n; 0 1 i++ ) { the first position of array i.e., letter must be converted to upper case. we checked this before second if statement as that is executed only when it encounters space and, there no a string. if( i="=" converting case using touppercase( in-built function ch[ ]="Character.toUpperCase(" ; need remove all spaces in between, check for empty ( \' incrementing counter by ctr++ immediately after + 1] continue loop } not encountered simply copy character else c++ new string will reduced have been removed thus returning with size return string.valueof( ch, 0, n - ctr driver code public static void main( args[ passing name method str="class test" system.out.println( convertstring( variable str1="employee" str2="this is the java tutorial" system.out.println(convertstring( < pre> <p> <strong>Output:</strong> </p> <pre> ClassTest Employee ThisIsTheJavaTutorial </pre> <hr></n;>