Bonjour tout le monde;
Voilà, j'ai appris en classe le cryptage de César (ou de décalage) et j'ai écrit un programme qui me fait un déchiffrement des mots que l'utilisateur aura entré. Le problème (qui est assez bête quand même) est que le caractére espace n'est pas pris en compte, quand un utilisateur entre deux mots séparés par le caractére espace, dans le résultat, mon programme change le caractère espace en 'a', mais si je tape seulement un mot à déchiffrer, le programme marche parfaitement!
Voici le programme en question:
Remarque: J'ai utilisé une classe lire pour les entrées au clavier au lien du scanner; voici son code:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51 public class Cesar { public static void main(String[] args) { char alphabet[] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'g', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' }; String texte; char cle; int i, j, key_value = 0, value; System.out.println("Veillez entrer le texte à déchiffrer"); texte = Lire.S(); int[] element = new int[texte.length()]; char[] dechiffre = new char[texte.length()]; System.out.println("Donnez la valeur de la clef de dechiffrement"); cle = Lire.c(); for (j = 0; j < alphabet.length; j++) { if (cle == alphabet[j]) { key_value = j; } } System.out.println("La clef de déchiffrement vaut " + key_value); for (i = 0; i < texte.length(); i++) { for (j = 0; j < alphabet.length; j++) { if (texte.charAt(i) == alphabet[j]) { value = j - key_value; if (value < 0) { value = 26 + value; } else { value = value; } if (value >= 26) { element[i] = value % 26; } else { element[i] = value; } } } } for (i = 0; i < texte.length(); i++) { if (texte.charAt(i) == ' ') { dechiffre[i] = texte.charAt(i); } for (j = 0; j < alphabet.length; j++) { if (element[i] == j) { dechiffre[i] = alphabet[j]; System.out.print(dechiffre[i]); } } } } }
Merci de bien vouloir me filer un coup de main
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112 import java.io.*; public class Lire { public static String S() // Lire un String { String tmp = ""; char C='\0'; try { while ((C=(char) System.in.read()) !='\n') { if (C != '\r') tmp = tmp+C; } } catch (IOException e) { System.out.println("Erreur de frappe"); System.exit(0); } return tmp; } // fin de S() public static byte b() // Lire un entier byte { byte x=0; try { x=Byte.parseByte(S()); } catch (NumberFormatException e) { System.out.println("Format numérique incorrect"); System.exit(0); } return x ; } public static short s() // Lire un entier short { short x=0; try { x=Short.parseShort(S()); } catch (NumberFormatException e) { System.out.println("Format numérique incorrect"); System.exit(0); } return x ; } public static int i() // Lire un entier { int x=0; try { x=Integer.parseInt(S()); } catch (NumberFormatException e) { System.out.println("Format numérique incorrect"); System.exit(0); } return x ; } public static long l() // Lire un entier long { long x=0; try { x=Integer.parseInt(S()); } catch (NumberFormatException e) { System.out.println("Format numérique incorrect"); System.exit(0); } return x ; } public static double d() // Lire un double { double x=0.0; try { x=Double.valueOf(S()).doubleValue(); } catch (NumberFormatException e) { System.out.println("Format numérique incorrect"); System.exit(0); } return x ; } public static float f() // Lire un float { float x=0.0f; try { x=Double.valueOf(S()).floatValue(); } catch (NumberFormatException e) { System.out.println("Format numérique incorrect"); System.exit(0); } return x ; } public static char c() // Lire un caractere { String tmp=S(); if (tmp.length()==0) return '\n'; else { return tmp.charAt(0); } } }![]()
Partager