Corrige TD-String Exercice 1 public class TestString { public static int string
Corrige TD-String Exercice 1 public class TestString { public static int stringCompare(String str1, String str2) { int l1 = str1.length(); int l2 = str2.length(); int lmin = Math.min(l1, l2); for (int i = 0; i < lmin; i++) { int str1_ch = (int)str1.charAt(i); int str2_ch = (int)str2.charAt(i); if (str1_ch != str2_ch) { return str1_ch - str2_ch; } } // Edge case for strings like // String 1="Geeks" and String 2="Geeksforgeeks" if (l1 != l2) { return l1 - l2; } // If none of the above conditions is true, // it implies both the strings are equal else { return 0; } } public static void main(String[] args) { // Première méthode avec compareTo String ch= "ABC!"; String ch1= "ACE!"; String ch2= "ABCDE"; System.out.println("Comparison = " + ch1.compareTo(ch2)); // ch1 > ch2 System.out.println("Comparison = " + ch2.compareTo(ch1)); // ch1 < ch2 System.out.println("Comparison = " + ch1.compareTo(ch1)); // Deuxième méthode en appelant StringCompare System.out.println("Comparison avec Meth 2 "); if (TestString.stringCompare(ch,ch2) < 0) System.out.println(ch + "est plus 'petit' que " + ch2); } } Exercice 2 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ù ch2 figure dans ch * @param ch chaîne de départ * @param ch2 sous-chaîne à chercher * @return occurence de ch2 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 ch2) { int nb =0; // nb de fois où ch2 figure dans ch int n =0; // indexe de démarrage de la recherche 'start' while (true){ n = ch.indexOf(ch2,n); // on cherche l'indexe où ch2 existe if (n == -1) break; // il n'y a plus de ch2 dans ce qui reste de la chaîne nb ++; n++; } return nb; } /** * Retourne le nb de fois où c figure dans ch * @param ch chaîne de départ * @param c un caractère à chercher * @return occurence de c dans ch */ public static int occCount(String ch, char c) { int nb =0; int n =0; while (true){ n = ch.indexOf(c,n); if (n == -1) 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 + 1; } return nb; } public static void main(String[] args) { String ch= "Hello world Hello Hello!"; String ch1= "world!"; String ch2= "Hello"; int n = StringUtil.occCount(ch,ch2); 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); } } Exercice 3 /* * Cryptage de César * [http://lwh.free.fr/pages/algo/crypto/cesar.htm] */ public class Cesar { static char[] alphabet1 = {'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 même taille * On prend chaque lettre du string * On prend sa position actuelle dans l'alphabet * On calcule sa nouvelle position en fonction de n * On va chercher dans l'alphabet la lettre correspondant à la nouvelle position * On assigne cette lettre dans le tableau de sortie au mm emplacement * On transforme le tableau Out en un string */ char[] charSIn = sIn.toCharArray(); char[] charSOut = new char[charSIn.length]; int pos1, pos2; for(int i = 0; i < charSIn.length; i++) { pos1 = posChar(charSIn[i], alphabet1); pos2 = newPos(pos1, n); if(pos2 == -1) charSOut[i] = ' '; // si -1, c'est que ce n'est pas une lettre, on met un espace à la place else charSOut[i] = alphabet1[pos2]; } return new String(charSOut); // on fait un string avec le tableau de char } public static String decrypt(int n, String sIn) { /* * La marche a suivre est la meme que pour le crypt sauf qu'on prend un n' = -n; */ char[] charSIn = sIn.toCharArray(); char[] charSOut = new char[charSIn.length]; int pos1, pos2; for(int i = 0; i < charSIn.length; i++) { pos1 = posChar(charSIn[i], alphabet1); pos2 = newPos(pos1, -n); if(pos2 == -1) charSOut[i] = ' '; // si -1, c'est que ce n'est pas une lettre, on met un espace à la place else charSOut[i] = alphabet1[pos2]; } return new String(charSOut); // on fait un string avec le tableau de char } // Renvoie la position du caractere dans le tableau // -1 si il n'est pas dans le tableau private static int posChar(char c, char[] tab) { for(int i = 0; i < tab.length; i++) { if(tab[i] == c) return i; } return -1; } // Donne la nouvelle position dans l'alphabet en fonction de n private static int newPos(int pos,int n) { int pos2 = pos; if(pos <= -1) { // -1 signifie que le caractere n'a pas été trouvé dans l'alphabet (caractere spécial, chiffre, espace, etc.) pos2 = -1; } else { int i = 0; while(i < abs(n)) { if(n < 0) { if(pos2 - 1 == -1) pos2 = 25; else pos2--; } else { if(pos2 + 1 >= 25) pos2 = 0; else pos2++; } i++; } } return pos2; } // Valeur absolue de a public static int abs(int a) { if(a >= 0) return a; else return -a; } public static void main(String[] args) { String t = "test de crypt"; System.out.println(crypt(3,t)); } } Exercice 4 public class Lexico { public enum Token{PAR_OUV, PAR_FERM, CROCH_OUV, CROCH_FERM, ACC_OUV, A CC_FERM, FIN, AUTRE} ; String chaine; public Lexico(String chaine) { this.chaine = chaine; } public Lexico.Token getToken(int i) { Token tok; switch(chaine.charAt(i)) { case '(': tok = Lexico.Token.PAR_OUV; break; case ')': tok = Lexico.Token.PAR_FERM; break; case '[': tok = Lexico.Token.CROCH_OUV; break; case ']': tok = Lexico.Token.CROCH_FERM; break; case '{': tok = Lexico.Token.ACC_OUV; break; case '}': tok = Lexico.Token.ACC_FERM; break; case ';': tok = Lexico.Token.FIN; break; default: tok = Lexico.Token.AUTRE; break; } return tok; } } /***PILE.java****/ import java.util.*; public class Pile { ArrayList chaine; public void empiler(Lexico.Token tok) { chaine.add(tok); } public Lexico.Token depiler() { if(chaine.size() == 0) { System.out.println("Mauvais paranthésage"); System.exit(1); } Lexico.Token tok = (Lexico.Token)chaine.get(chaine.size()-1); chaine.remove(chaine.size()-1); return tok; } public Lexico.Token sommet() { if(chaine.size() == 0) { System.out.println("Mauvais paranthésage"); System.exit(1); } Lexico.Token tok = (Lexico.Token)chaine.get(chaine.size()-1); return tok; } public Pile() { chaine = new ArrayList<Lexico.Token>(); } } /***SYNTAX.java*****/ import java.util.*;; public class Syntax { String chaine; public Syntax(String chaine) { this.chaine = chaine; } public boolean analyse() { Pile heap = new Pile(); Lexico lex = new Lexico(chaine); int i = 0; boolean resultat = true; Lexico.Token tok = null; while(tok != Lexico.Token.FIN) { tok = lex.getToken(i); if(tok == Lexico.Token.PAR_OUV) heap.empiler(Lexico.Token.PAR_FERM); else if(tok == Lexico.Token.CROCH_OUV) heap.empiler(Lexico.Token.CROCH_FERM); else if(tok == Lexico.Token.ACC_OUV) heap.empiler(Lexico.Token.ACC_FERM); else if(tok == Lexico.Token.PAR_FERM) { if(tok == heap.sommet()) heap.depiler(); else resultat = false; } else if(tok == Lexico.Token.CROCH_FERM) { if(tok == heap.sommet()) heap.depiler(); else resultat = false; } else if(tok == Lexico.Token.ACC_FERM) { if(tok == heap.sommet()) heap.depiler(); else resultat = false; } i++; } return resultat; } public static void main(String args[]) { System.out.println("Entrer une chaine et terminer par ;"); Scanner clavier = new Scanner(System.in); String chaine = clavier.nextLine(); Syntax syn = new Syntax(chaine); if(syn.analyse()) System.out.println("Bonne analyse!!!"); else System.out.println("Mauvais paranthésage!!!"); } } uploads/Religion/ corrigetd-string.pdf
Documents similaires






-
62
-
0
-
0
Licence et utilisation
Gratuit pour un usage personnel Attribution requise- Détails
- Publié le Dec 14, 2021
- Catégorie Religion
- Langue French
- Taille du fichier 0.1101MB