logo

Kupranugarių dėklas Java kalba

„Java“ vadovaujasi kupranugarių didžiųjų ir mažųjų raidžių sintaksė pavadindama klases, sąsajas, metodus ir kintamuosius. Jei pavadinimas derinamas su dviem žodžiais, antrasis žodis visada prasidės didžiosiomis raidėmis, pvz., maxMarks( ), pavardė, klasės testas, pašalinant visus tarpelius.

Yra du būdai, kaip naudoti „Camel“ dėklą:

  1. Mažosios kupranugario didžiosios raidės, kai pirmasis pirmojo žodžio simbolis yra mažosiomis raidėmis. Šios taisyklės paprastai laikomasi įvardijant metodus ir kintamuosius. Pavyzdys, vardas, pavardė, actionEvent, printArray( ) ir kt.
  2. Didžioji kupranugario didžioji raidė taip pat žinoma kaip pavadinimo didžioji raidė, kai pirmasis pirmojo žodžio simbolis yra didžiosios raidės. Šios taisyklės paprastai laikomasi įvardijant klases ir sąsajas. Pavyzdžiui, darbuotojas, spausdinamas ir kt.

Įprastos eilutės pavertimas kupranugario dėklu

Eilutę galima konvertuoti į apatinį arba viršutinį kupranugarių dėklą tiesiog pašalinus tarpelius iš eilutės.

Apatinės kupranugario dėklo pavyzdys:

Įvestis: JavaTpoint yra geriausia programavimo kalbų mokymo svetainė.

Išvestis: javaTpointIsTheBestTutorialSiteForProgrammingLanguages.

Viršutinio kupranugario dėklo pavyzdys:

Įvestis: tai yra java pamoka

Išvestis: ThisIsTheJava Tutorial

Algoritmas:

  1. Pereikite simbolių masyvą po simbolio, kol jis pasiekia pabaigą.
  2. Pirmoji eilutės raidė, kurios indeksas = 0, paverčiama mažosiomis raidėmis (sekant mažosiomis kupranugarių didžiosiomis raidėmis) arba didžiosiomis (kai seka didžiosiomis kupranugarių raidėmis).
  3. Patikrinama, ar masyve nėra tarpų, o iškart po tarpo esanti raidė paverčiama didžiosiomis raidėmis.
  4. Jei aptinkamas ne tarpo simbolis, jis nukopijuojamas į gautą masyvą.

Įdiegkime algoritmą Java programoje.

A. Stygos konvertavimas į apatinį kupranugario dėklą

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. Stygos konvertavimas į viršutinį kupranugario dėklą

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