d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Appends > Appends For Me Appends For You
Add Reply New Topic New Poll
Member
Posts: 2,866
Joined: Nov 4 2006
Gold: 0.00
Feb 28 2014 08:35pm
char* stringCat(char* str1, const char* str2);
Appends a copy of string str2 to string str1. The first character of str2 overwrites the terminating null character of str1. The value of str1 is returned.

no cstring lib
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Feb 28 2014 08:46pm
Quote (evilironman @ Feb 28 2014 10:35pm)
char* stringCat(char* str1, const char* str2);
Appends a copy of string str2 to string str1. The first character of str2 overwrites the terminating null character of str1. The value of str1 is returned.

no cstring lib


Whats the problem? Provide your usage/code as well as error messages. Are you trying to come up with your own version of an existing function? What language are you using C or C++.

This is the most useless thread I've read to date...

Take a look at: http://www.cplusplus.com/reference/cstring/strcat/ it looks like you just spelt it wrong.

Here is a untested version that does not use cstring functions, if this is what you are after. The code is bad and might not even work. It also forces the user to have a pointer waiting for the function as well as forces the user to clean up the allocated memory once done using it. Also if either string does not contain a nullbyte the function would break.

Code
char* stringCat(char* string1, char* string2) {
int len1 = 0, len2 = 0;
char* result = null;

while(string1++) { len1++ }
while(string2++) { len2++ }

result = (char*)malloc((len1 + len2) * sizeof(char));

while(--len1>0) { result[len1] = string1[len1]; }
while(len2-->0) { result[len2] = string2[len2]; }

return result;
}


This post was edited by AbDuCt on Feb 28 2014 09:04pm
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Feb 28 2014 09:00pm
Seems to me like he wants us to implement it for him.
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Feb 28 2014 09:04pm
Quote (Minkomonster @ Feb 28 2014 11:00pm)
Seems to me like he wants us to implement it for him.


Yea I assume so. I threw in a crude version which I haven't tested. Would give the general idea though.
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Feb 28 2014 09:51pm
Quote (AbDuCt @ Feb 28 2014 09:46pm)
Whats the problem? Provide your usage/code as well as error messages. Are you trying to come up with your own version of an existing function? What language are you using C or C++.

This is the most useless thread I've read to date...

Take a look at: http://www.cplusplus.com/reference/cstring/strcat/ it looks like you just spelt it wrong.

Here is a untested version that does not use cstring functions, if this is what you are after. The code is bad and might not even work. It also forces the user to have a pointer waiting for the function as well as forces the user to clean up the allocated memory once done using it. Also if either string does not contain a nullbyte the function would break.

Code
char* stringCat(char* string1, char* string2) {
    int len1 = 0, len2 = 0;
    char* result = null;

    while(string1++) { len1++ }
    while(string2++) { len2++ }

    result = (char*)malloc((len1 + len2) * sizeof(char));

    while(--len1>0) { result[len1] = string1[len1]; }
    while(len2-->0) { result[len2] = string2[len2]; }

    return result;
}


That isn't technically appending though

Code
char* stringCat(char* string1, const char* string2)
{
int i, j;
for(i=0;string1[i];i++);
for(j=0;string2[j];j++) string1[i+j]=string2[j];
string1[i+j] = '\0';

return string1;
}


Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Feb 28 2014 10:12pm
Quote (Minkomonster @ Feb 28 2014 11:51pm)
That isn't technically appending though

Code
char* stringCat(char* string1, const char* string2)
{     
    int i, j;
    for(i=0;string1[i];i++);
    for(j=0;string2[j];j++) string1[i+j]=string2[j];
    string1[i+j] = '\0';

    return string1;
}


Wouldn't that be unsafe though seeing as string1 may not have enough allocated space to house string2. But yea you're right, my method doesn't really append to string1, but rather returns a new string with the two concated.

This post was edited by AbDuCt on Feb 28 2014 10:12pm
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Feb 28 2014 11:04pm
Quote (AbDuCt @ Feb 28 2014 11:12pm)
Wouldn't that be unsafe though seeing as string1 may not have enough allocated space to house string2. But yea you're right, my method doesn't really append to string1, but rather returns a new string with the two concated.


Yes, I was just giving an example of how it would be done with appends.Though I am not going to take the time to deal with bounds checking, mallocs, and all that crazy shit for some random who couldn't even give us the decency of a proper post. While we are on the subject, I am assuming strcat does the same thing seeing as how it requests the "dest" string be large enough to contain the concat string
http://www.cplusplus.com/reference/cstring/strcat/


I guess you could go crazy with pointers too

Code
char* strcat(char *dest, const char *src)
{
while(*dest++);
dest--;
while(*dest++=*src++);
return dest;
}


This post was edited by Minkomonster on Feb 28 2014 11:13pm
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Feb 28 2014 11:38pm
Quote (Minkomonster @ Mar 1 2014 01:04am)
Yes, I was just giving an example of how it would be done with appends.Though  I am not going to take the time to deal with bounds checking, mallocs, and all that crazy shit for some random who couldn't even give us the decency of a proper post. While we are on the subject, I am assuming strcat does the same thing seeing as how it requests the "dest" string be large enough to contain the concat string
http://www.cplusplus.com/reference/cstring/strcat/


I guess you could go crazy with pointers too

Code
char* strcat(char *dest, const char *src)
{
  while(*dest++);
  dest--;
  while(*dest++=*src++);
  return dest;
}


Yay pointers.


Go Back To Programming & Development Topic List
Add Reply New Topic New Poll