logo

Kamieļu futrālis Java valodā

Klašu, saskarņu, metožu un mainīgo nosaukšanai Java ievēro kamieļu reģistra sintakse. Ja nosaukums ir apvienots ar diviem vārdiem, otrais vārds vienmēr sāksies ar lielo burtu, piemēram, maxMarks( ), LastName, ClassTest, noņemot visas atstarpes.

Ir divi Camel korpusa izmantošanas veidi:

  1. Mazais kamieļa burts, kur pirmā vārda pirmā rakstzīme ir ar mazo burtu. Šo konvenciju parasti ievēro, nosaucot metodes un mainīgos. Piemērs, vārds, uzvārds, actionEvent, printArray() utt.
  2. Kamieļu augšējais burts, kas pazīstams arī kā virsraksta burts, kur pirmā vārda pirmā rakstzīme ir ar lielo burtu. Šī konvencija parasti tiek ievērota, nosaucot klases un saskarnes. Piemēram, Darbinieks, Drukājams utt.

Parastas virknes pārvēršana kamieļa futrālī

Virkni var pārvērst par apakšējo vai augšējo kamieļa korpusu, vienkārši noņemot no virknes atstarpes.

Apakšējā kamieļa korpusa piemērs:

Ievade: JavaTpoint ir labākā programmēšanas valodu apmācības vietne.

Izvade: javaTpointIsTheBestTutorialSiteForProgrammingLanguages.

Augšējā kamieļa korpusa piemērs:

Ievade: šī ir java apmācība

Izvade: ThisIsTheJava Tutorial

Algoritms:

  1. Pārvietojiet rakstzīmju masīvu pēc rakstzīmes, līdz tas sasniedz beigas.
  2. Virknes pirmais burts ar indeksu = 0 tiek pārveidots par mazajiem burtiem (kad seko kamieļu mazajiem burtiem) vai par lielo burtu (kad seko kamieļu lielajiem burtiem).
  3. Masīvs tiek pārbaudīts, vai nav atstarpes, un burts tūlīt aiz atstarpes tiek pārveidots par lielo burtu.
  4. Ja tiek atrasta rakstzīme, kas nav atstarpe, tā tiek kopēta iegūtajā masīvā.

Realizēsim algoritmu Java programmā.

A. Virknes pārveidošana par Camel Case

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. Virknes pārveidošana par augšējo kamieļa korpusu

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;>