Ex:
string : My first example to count pattern in my Java using my eclipse.
pattern : my
count : 3
public class PatternMatching {
public static void main(String args[]) {
String myString = "My first example to count pattern in my Java using my eclipse.";
char[] chararr = myString.toLowerCase().toCharArray();
// without predefined function
findMatch(chararr);
split();
}
private static void findMatch(char[] chararr) {
String pattern = "my";
char[] patternArr = pattern.toLowerCase().toCharArray();
int patternArrlen = patternArr.length;
int f = 0;
for (int k = 0; k < chararr.length; k++) {
int j = 0;
for (int z = 0; z < patternArrlen; z++) {
if (patternArr[z] == chararr[k]) {
j = j + 1;
k = k + 1;
if (j == patternArrlen) {
f = f + 1;
}
}
}
}
System.out.println(f);
}
static void split() {
String myString = "This is my first prgm to my pattern count in my java.";
String pattern = "my";
String[] seperateWords = myString.split(" ");
int i = 0;
for (int j = 0; j < seperateWords.length; j++) {
if (seperateWords[j].equalsIgnoreCase(pattern)) {
i++;
}
}
System.out.println(i);
}
}
No comments:
Post a Comment