Re: End of String
"Nigel Wade" <nmw@ion.le.ac.uk> wrote in message
news:efbjq8$p74$1@south.jnrs.ja.net...
Even more pointless, but not using length() per your original request :
static int strlen(String str) {
int i=0;
try {
while(true) {
str.getChar(i);
i++;
}
}
catch(IndexOutOfBoundsException) {
return i;
}
}
Are they enough to meet your requirements?
I was trying to come up with the "most pointless" way to solve the
requirement, in case Sara persisted.
Best I came up with is using a recursive method which generates every
single possible string of known length, and checks whether the generated
string matches the provided string.
<pseudoCode>
int getStringLength(String target) {
int length = 0;
while(true) {
if (stringEquals(target, "", length)) {
return length;
}
length++;
}
}
boolean stringEquals(String target, String prefix, int length) {
if (length == 0) {
return target.equals(prefix);
}
for (char nextChar : allPossibleCharacters()) {
if (stringEquals(target, prefix + nextChar, length - 1) {
return true;
}
}
return false;
}
</pseudoCode>
- Oliver