palindrome function
ex
exeqtor at gmail.com
Sat Aug 7 16:56:08 PDT 2010
Hello!
I'm learning D and I was wondering on what can be optimized of my
implementation of a palindrome function:
bool isPalindrome(int num) {
string s = text(num);
int len = s.length;
for (int i = 0; i < len / 2; ++i) {
if (s[i] != s[len - 1 - i]) {
return false;
}
}
return true;
}
1) Is std.conv.text() the best function to call here to convert a number to
string?
2) Does using a variable to store the string length is more efficient than
calling the length method?
3) This example use numeric characters but if the string were UTF8 the []
operator would be OK for multibyte characters?
Not related:
4) Is some way to declare the nested functions at the end, this way I could
create nice "routines" of a function.
5) I could not find in the documentation if when I declare a variable inside a
block (a loop for example) that variable creation is optimized by the compiler.
More information about the Digitalmars-d-learn
mailing list