Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

any problems with this code

klalaklala Posts: 2Member
Folks, I have been told that there is one logical problem with this code. Basically, this is an implementation of the library function strcat().

The intended beahviour of this function to concatenate ct to the end of s. Return s.

I could not find any logical problems with the code, apart from the fact that the final string should be NULL terminated, but I am not sure that can be classified as an error.

Could you help me find the error, if there is any.

char *strcat(char *s, const char *ct)
{
int i, start;
start = strlen(s);
for (i = 0; i < strlen(ct); i++)
{
s[start + i] = ct[i];
}
return s;
}


Comments

  • LundinLundin Posts: 3,711Member
    I can't see any obvious error there, except the lack of null termination, which is a big bug.

    This line is very inefficient (but it will work):

    for (i = 0; i < strlen(ct); i++)

    A better way is do only call strlen once before the loop.
  • losthelperlosthelper Posts: 35Member
    your logical error is here

    [code]
    for (i = 0; i < strlen(ct); i++)// this care only for the lenght of ct
    {
    s[start + i] = ct[i];// start+i(s) is not greater than array size
    }
    [/code]
    I am in relearning process, sorry if i make a mistake
  • AsmGuru62AsmGuru62 Posts: 6,519Member
    [color=Blue]Not sure about this...

    If you start using strlen () on a not finished string - you may get a crash.[/color]
  • LundinLundin Posts: 3,711Member
    : your logical error is here
    :
    : [code]:
    : for (i = 0; i < strlen(ct); i++)// this care only for the lenght of ct
    : {
    : s[start + i] = ct[i];// start+i(s) is not greater than array size
    : }
    : [/code]:


    No that looks fine, start is used as an offset to determine where the "s" string ends.
  • LundinLundin Posts: 3,711Member
    : [color=Blue]Not sure about this...
    :
    : If you start using strlen () on a not finished string - you may get
    : a crash.[/color]

    strlen is used on ct only, which is copied and remains untouched by the function. If it was used on the string that is built up, there would be issues.

    There would perhaps also be an issue if both strings point at the same place in memory, but that is the responsibility of the caller to avoid. Of course, both strings need to be null terminated, but that is also the responsibility of the caller.
  • losthelperlosthelper Posts: 35Member
    s[5]= "cat"
    a[5] = "and"


    call this function with s a

    strcat(s , a)

    this function will do

    s = "catand";
    wouldn't that be greater than the 5 and will be writting in other memory allocation. That is my point of view. strncat is a better choose to never write more than you have to.

    *** correction
    s[start + i] = ct[i];// start+i is not greater than array size of s
    ****************
    I am in relearning process, sorry if i make a mistake
  • LundinLundin Posts: 3,711Member
    Yes that is a problem, but it isn't strcat()'s problem, it is something the caller must solve.
  • tcanvntcanvn Posts: 6Member
    Hi alls,

    Support that there is no problem with the size of s, we should add: [b]s[start + i] = '';[/b] after the for loop as follow:

    [code]
    char *strcat(char *s, const char *ct)
    {
    int i, start;
    start = strlen(s);
    for (i = 0; i < strlen(ct); i++)
    s[start + i] = ct[i];

    [color=Red][b]s[start + i] = '';[/b][/color]

    return s;
    }

    [/code]

    Regards.

  • ArcaizArcaiz Posts: 2Member
    How about this?



    [code]
    char *strcat(char *s, char *ct)[/code]
  • tcanvntcanvn Posts: 6Member
    it is alright but it is not good as the above.

    why? the second parameter should not be changed inside the function. so, we should declare it as a [color=Red]const[/color] parameter.
Sign In or Register to comment.