by Locke1689 · 16y ago · 0 comments
Node * iter;
for (iter=root; iter != NULL; iter=iter->next) {
/* iter->object; */
}
A concise implementation of strlen size_t strlen(char * str) {
char * cur;
for(cur=str; *cur; ++cur);
return (cur-str);
}
Reverse a string in-place. void reverse(char * str) {
char *i,*j, tmp;
for (i=str, j=(str+strlen(str)-1); i < j; ++i,++j) {
tmp = *i;
*i = *j;
*j = tmp;
}Anyhow, when asked to write those on a blackboard, I typically do this:
size_t strlen(char* start) {
char* end=start;
while(*end) ++end;
return (end-start);
}
and void reverse(char* i) {
char* j=(i+strlen(i)-1);
for (; i < j; ++i,--j) {
*i ^= *j;
*j ^= *i;
*i ^= *j;
}Also, the XOR version probably isn't worth the complexity.
for(cur=str; cur; ++cur);
should be: for(cur=str; *cur; ++cur); size_t strlen(char *s)
{
size_t i = 0;
while(*s++) i++;
return i;
}