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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
| package aide.forum.developpez;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
private static Scanner sc = null;
public static void main(String[] args) {
sc = new Scanner(System.in);
while (true) {
processTwoString();
}
}
private static void processTwoString() {
System.out.println("Bonjour, entrez deux chaînes de caractères");
String stringA = getStringA();
String stringB = getStringB(stringA);
int count = 0;
count = getCountOfBInAByRegex(stringA, stringB);
displayResult(stringA, stringB, count, "regex");
count = getCountOfBInAByHand(stringA, stringB);
displayResult(stringA, stringB, count, "hand");
}
/**
* @param stringA The string we want to seek in
* @param stringB The string we want to seek
* @return the number of occurrence
*/
private static int getCountOfBInAByHand(String stringA, String stringB) {
int count = 0;
char[] charTableA = stringA.toCharArray();
char[] charTableB = stringB.toCharArray();
for (int i = 0; i < charTableA.length; i++) {
boolean equals = true;
for (int j = 0; j < charTableB.length; j++) {
int nextIndex = i+j;
if(charTableA.length<nextIndex+1){
equals = false;
break;
}
if(equals && charTableA[nextIndex] != charTableB[j]){
equals = false;
}
}
if (equals) {
count++;
}
}
return count;
}
/**
* @param stringA The string we want to seek in
* @param stringB The string we want to seek
* @param count The number of occurrence of stringB in stringB
* @param methode The method use to find count
*/
private static void displayResult(String stringA, String stringB,
int count, String method) {
System.out.println("Methode " + method + " : Il a " + count
+ " fois la chaîne \"" + stringB + "\" dans \"" + stringA
+ "\"");
}
/**
* @param stringA The string we want to seek in
* @param stringB The string we want to seek
* @return the number of occurrence of stringB in stringB
*/
private static int getCountOfBInAByRegex(String stringA, String stringB) {
Pattern pattern = Pattern.compile(stringB);
Matcher matcher = pattern.matcher(stringA);
int count = 0;
while (matcher.find()) {
count++;
}
return count;
}
/**
* @param stringA The string we want to seek in
* @return The string we want to seek
*/
private static String getStringB(String stringA) {
int longeurMaxChainB = stringA.length() - 1;
System.out.println("Entrez une chaîne dune longueur maximum de "
+ longeurMaxChainB + " caractères.");
String toReturn = null;
while (true) {
toReturn = sc.nextLine();
if (toReturn.length() > longeurMaxChainB) {
System.out
.println("La chaîne doit être dune longueur maximum de "
+ longeurMaxChainB + " caractères.");
} else if (toReturn.length() < 1) {
System.out
.println("La chaîne doit être dune longueur minimum de 1 caractères.");
} else {
break;
}
}
return toReturn;
}
/**
* @return The String we want to seek in
*/
private static String getStringA() {
System.out
.println("Entrez une chaîne dune longueur minimum de 4 caractères.");
String toReturn = null;
while (true) {
toReturn = sc.nextLine();
if (toReturn.length() < 4) {
System.out
.println("La chaîne doit être dune longueur minimum de 4 caractères.");
} else {
break;
}
}
return toReturn;
}
} |
Partager