Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 C TYS
Index -> Programming, C++ -> C++ Help
Goto page Previous  1, 2, 3  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
[Gandalf]




PostPosted: Tue Dec 12, 2006 11:02 pm   Post subject: (No subject)

Nope, no edit button in help forums because it was abused.
wtd wrote:
Much simpler than that, Gandalf. Smile

Do you understand how the strcpy function works?

Well, I assumed so. Razz Even simpler than Andy's too? Something with recursion maybe?

And yes, I'm quite confident in my understanding of your strcpy function.
Sponsor
Sponsor
Sponsor
sponsor
md




PostPosted: Wed Dec 13, 2006 12:51 am   Post subject: (No subject)

c++:

char *cat(char *dest, char *src)
{
    while( *dest ) dest++;
    while( *dest++ = *src++ ) ;
    return dest;
}


please excuse any typos... beer makes typing hard.
Andy




PostPosted: Wed Dec 13, 2006 5:44 am   Post subject: (No subject)

ohh, multiple lines! lol.

md, you cant return a pointer when your functions shifts it. but i'm sure that was just the beer thinking =P
bugzpodder




PostPosted: Wed Dec 13, 2006 9:29 am   Post subject: (No subject)

Andy wrote:
tsk tsk gandalf...

c++:

void strcat(char *dest, char *src)
{
    while (*dest++ = (*dest)?*dest:*src++);
}




tsk tsk Andy...

c++:

while ((*d++) || *d = *s++);
Monstrosity_




PostPosted: Wed Dec 13, 2006 11:43 am   Post subject: (No subject)

Andy wrote:
md, you cant return a pointer when your functions shifts it.

And why not?
md




PostPosted: Wed Dec 13, 2006 11:43 am   Post subject: (No subject)

Andy wrote:
ohh, multiple lines! lol.

md, you cant return a pointer when your functions shifts it. but i'm sure that was just the beer thinking =P


Sure you can! though it should really be return dest--;

I think bugzpodder's code is the shorted however...
wtd




PostPosted: Wed Dec 13, 2006 12:15 pm   Post subject: (No subject)

md wrote:
I think bugzpodder's code is the shorted however...


And yet represents more of a change than the code I demonstrated to you. Wink
bugzpodder




PostPosted: Wed Dec 13, 2006 12:44 pm   Post subject: (No subject)

the correct code should actually be:
[syntax]
while(*++d||(*d=*s++));
[/code]

because of a subtlety *++d is evaluated from left to right, so d gets deferenced first then increased

Quote:
And yet represents more of a change than the code I demonstrated to you.
What do you mean?
Sponsor
Sponsor
Sponsor
sponsor
Monstrosity_




PostPosted: Wed Dec 13, 2006 1:00 pm   Post subject: (No subject)

bugzpodder wrote:
the correct code should actually be:
[syntax]
while(*++d||(*d=*s++));
[/code]

because of a subtlety *++d is evaluated from left to right, so d gets deferenced first then increased

The grouping for those two is right to left, (*(++d)). If it were (++(*d)), where d gets dereferenced first then incremented, then your stuck in loop where the pointer never changes.

Also, your increments in both examples would skip assigning/checking to the first value in the array. Try with d as an empty string.

wtd wrote:
md wrote:
I think bugzpodder's code is the shorted however...

And yet represents more of a change than the code I demonstrated to you. Wink

Hmm, I'm interested in what yours could be now. I would have just added another line to the strcpy function.
bugzpodder




PostPosted: Wed Dec 13, 2006 1:23 pm   Post subject: (No subject)

I was too greedy. Tried to put everything in the conditional statement.

code:

while(*d||(*d=*s++))d++;


Take that, punk! Razz
bugzpodder




PostPosted: Wed Dec 13, 2006 1:28 pm   Post subject: (No subject)

and before anyone claims andy's code is shorter, both code sets has comparable character counts, and my code do |d| less assignments than andy's code
wtd




PostPosted: Wed Dec 13, 2006 1:33 pm   Post subject: (No subject)

code:
void strcat(char *dest, char *src)
{
   while (*dest) dest++;
   while (*dest++ = *src++);
}


This represents a very minimal change to the strcpy function as provided.
Monstrosity_




PostPosted: Wed Dec 13, 2006 1:40 pm   Post subject: (No subject)

wtd wrote:
code:
void strcat(char *dest, char *src)
{
   while (*dest) dest++;
   while (*dest++ = *src++);
}


This represents a very minimal change to the strcpy function as provided.



I would have done
code:
void strcat(char *dest, char *src)
{
   dest += strlen(dest);
   while (*dest++ = *src++);
}

same deal, just function call over head ;p
bugzpodder




PostPosted: Wed Dec 13, 2006 1:44 pm   Post subject: (No subject)

then you might as well use memcpy
wtd




PostPosted: Wed Dec 13, 2006 2:00 pm   Post subject: (No subject)

Monstrosity_ wrote:
code:
void strcat(char *dest, char *src)
{
   dest += strlen(dest);
   while (*dest++ = *src++);
}


This actually won't work. Wink

Anyone care to guess why?
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 2 of 3  [ 41 Posts ]
Goto page Previous  1, 2, 3  Next
Jump to:   


Style:  
Search: