Hi,
I have a school assignment where I need to check if one string is a substring in another string. However, we are not allowed to do any string-based comparison, we are only allowed to compare the ASCII values. So the way I do it now is to call the String.toCharArray() on both strings and use loops. However it looks pretty ugly, and I figure there has to be a better way to do this. This is what I have, and it is working but I wish to do it in a better way.
public boolean deltekst(String tekst1, String tekst2){
if(equals(text1, text2))
return false;
if(bigger(text2, text1))
return false;
char[] list1 = text1.toCharArray();
char[] list2 = text2.toCharArray();
boolean substring = false;
for(int i = 0; i < list1.length; i++){
if((int)list1[i] == (int)list2[0]){
for(int j = 1; j < list2.length; j++){
if((i + j) >= list1.length)
break;
else{
int ascii1 = (int)list[i + j];
int ascii2 = (int)list[j];
if(ascii1 != ascii2)
break;
else if(j == list2.length - 1)
substring = true;
}
}
}
}
return substring;
}
Thanks in advance.