supercowpower
Neu angemeldet
- Registriert
- 9 Feb. 2016
- Beiträge
- 25
Hi, ich vermisse ein Code Schnipsel Thread wie damals, in diesem Thread hier geht es um kleine Code Fragen.
---
Dann frage ich mal ^^
Sprache Java
[src=java]
// Harte Kritik am Code bitte
class Test {
public static void main(String[] args) {
String text = "blak f2ololr a su7bstlhelloolri7nheg in9me";
String keyword = "lol";
int keywordLength = 0;
int keywordCounter = 0;
{ // search for keyword
int max = text.length() - keyword.length();
int keywordIndex = 0;
int j = 0;
con:
for (int i = 0 ; i <= max ; ) {
keywordIndex = 0;
if (text.charAt(++i) != keyword.charAt(keywordIndex)) // if first letter of keyword not found, continue to next letter to check
continue con;
keywordLength = keyword.length(); // only for the following while loop parameter
// check the keyword match
while ( -1 < --keywordLength ) { // do the while-loop maximal (keyword.length()-1) times
if (text.charAt(i++) != keyword.charAt(keywordIndex++)) { // check whether the letters matching
continue con;
}
}
keywordCounter++;
}
// print results
System.out.println("Found \"" + keyword + "\" "
+ keywordCounter + " times");
} // end, search for keyword
}
}[/src]
---
Dann frage ich mal ^^
Sprache Java
[src=java]
// Harte Kritik am Code bitte
class Test {
public static void main(String[] args) {
String text = "blak f2ololr a su7bstlhelloolri7nheg in9me";
String keyword = "lol";
int keywordLength = 0;
int keywordCounter = 0;
{ // search for keyword
int max = text.length() - keyword.length();
int keywordIndex = 0;
int j = 0;
con:
for (int i = 0 ; i <= max ; ) {
keywordIndex = 0;
if (text.charAt(++i) != keyword.charAt(keywordIndex)) // if first letter of keyword not found, continue to next letter to check
continue con;
keywordLength = keyword.length(); // only for the following while loop parameter
// check the keyword match
while ( -1 < --keywordLength ) { // do the while-loop maximal (keyword.length()-1) times
if (text.charAt(i++) != keyword.charAt(keywordIndex++)) { // check whether the letters matching
continue con;
}
}
keywordCounter++;
}
// print results
System.out.println("Found \"" + keyword + "\" "
+ keywordCounter + " times");
} // end, search for keyword
}
}[/src]
Zuletzt bearbeitet: