Corrigetd string Corrige TD-String Exercice public class TestString public static int stringCompare String str String str int l str length int l str length int lmin Math min l l for int i i lmin i int str ch int str charAt i int str ch int str charAt i if

Corrige TD-String Exercice public class TestString public static int stringCompare String str String str int l str length int l str length int lmin Math min l l for int i i lmin i int str ch int str charAt i int str ch int str charAt i if str ch str ch return str ch - str ch Edge case for strings like String Geeks and String Geeksforgeeks if l l return l - l If none of the above conditions is true it implies both the strings are equal else return public static void main String args Première méthode avec compareTo String ch ABC String ch ACE String ch ABCDE System out println Comparison ch compareTo ch ch ch System out println Comparison ch compareTo ch ch ch CSystem out println Comparison ch compareTo ch Deuxième méthode en appelant StringCompare System out println Comparison avec Meth if TestString stringCompare ch ch System out println ch est plus 'petit' que ch Exercice La solution proposée ici utilise indexOf Mais il est possible d ? utiliser split ou StringTokenizer public class StringUtil Retourne le nb de fois o? ch ?gure dans ch param ch cha? ne de départ param ch sous-cha? ne à chercher return occurence de ch dans ch public static char alphabet 'a' 'b' 'c' 'd' 'e' 'f' 'g' 'h' 'i' 'j' 'k' 'l' 'm' 'n' 'o' 'p' 'q' 'r' 's' 't' 'u' 'v' 'w' 'x' 'y' 'z' public static int occCount String ch String ch int nb nb de fois o? ch ?gure dans ch int n indexe de démarrage de la recherche 'start' while true n ch indexOf ch n on cherche l'indexe o? ch existe if n - break il n'y a plus de ch dans ce qui reste de la cha? ne nb n return nb Retourne le nb de fois o? c ?gure dans ch param ch cha? ne de départ param c un caractère à chercher return occurence de c dans ch Cpublic static int occCount String ch char c int nb int n while true n ch indexOf c n if n - break nb n return nb public static int distinctCount String ch int nb ch length for char c alphabet int n occCount ch c nb nb -n return nb public static void main String args String ch Hello world Hello Hello String ch world String ch Hello int n StringUtil occCount ch ch System out println occurence sous-cha? ne n n StringUtil occCount ch 'H' System out println occurence caractère n n StringUtil distinctCount ch System out println distinctCount n CExercice Cryptage de César http lwh free fr pages algo crypto cesar htm public class Cesar static char alphabet 'a' 'b' 'c' 'd' 'e' 'f' 'g' 'h' 'i' 'j' 'k' 'l' 'm' 'n' 'o' 'p' 'q' 'r' 's' 't' 'u' 'v' 'w' 'x' 'y' 'z' public static String crypt int n String sIn On convertit le string en un tableau de char On crée un tableau Out de

  • 28
  • 0
  • 0
Afficher les détails des licences
Licence et utilisation
Gratuit pour un usage personnel Aucune attribution requise
Partager
  • Détails
  • Publié le Jul 21, 2022
  • Catégorie Religion
  • Langue French
  • Taille du fichier 47.6kB