I wrote a program that receives 2 strings (Input and Search) in the Search string. The sign '+' indicates that if the substring before the '+' exists in the Input string. (The search string cannot start with '+' and there cannot be a '+' followed by another '+').
Can you review it for best coding practices and efficiency?
boolean notgood = false;
boolean break1 = false;
boolean break2 = false;
int counter = 0;
if (search.charAt(0)=='+'||search.charAt(0)=='*') {
System.out.println("Invalid search striNG.");
continue;
}
//////////////////////////////////////////////
for (i=0; i<search.length() && notgood==false; i++) {
if (search.charAt(i)!='*' && search.charAt(i)!='+') {
counter++;
}
if (counter == search.length()) {
System.out.println("Invalid SEARCH string.");
notgood=true;
break;
}
if ((search.charAt(i)=='*')||(search.charAt(i)=='+')) {
if (i!=search.length()-1) {
if ((search.charAt(i+1)=='*')||(search.charAt(i+1)=='+')) {
System.out.println("INvalid search string.");
notgood=true;
break1=true;
}
}
}
}
////////////////////////////////////////////
for (i=0; i<search.length() && !break1; i++) {
int c=0;
if (search.charAt(i)=='+') {
String word = search.substring(0,i);
for (int j=0; j<input.length() && !break2; j++) {
if ((input.charAt(j) == word.charAt(c)) && c<word.length()) {
c++;
}
if (c>=word.length()) {
System.out.println("Search string matches input string.");
break1=true;
break2=true;
}
}
if (c<word.length()) {
System.out.println("Search string doesn't match input string.");
}
}
For example, for Input = 'abcd'
and Search = 'ab+cd+'
, the result should be the strings match.
notgood==false
when you can shorten it to!notgood
? Come to think of it, you could invert the semantics and replacenotgood
withisGood
. \$\endgroup\$ – DodgyCodeException Apr 16 '19 at 12:42