0

I made a function that concat string t to the end of string s, for my exercise I have to use pointers for this, so I did this way but its not working:

#include <stdio.h>

void strcat(char *s, char *t)
{
    while (*s++);

    for (*s = *t; *s = *t; s++, t++);
}

int main()
{
    char c[100] = "hello";
    char *w = " world\n";
    
    strcat(c, w);

    printf(c);
    
    return 0;
}

The output of c always return "hello" instead of "hello world\n"

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • `printf("%s\n", c)` try like this. – kiner_shah Jan 06 '22 at 11:49
  • Does this answer your question? [How to concat two char \* in C?](https://stackoverflow.com/questions/9746866/how-to-concat-two-char-in-c) – voiarn Jan 06 '22 at 11:49
  • 2
    Take this as the perfect time to learn how to use a *debugger* to step through your code statement by statement while monitoring variables and their values. – Some programmer dude Jan 06 '22 at 11:49
  • 2
    The evaluation that *fails* the `while (*s++);` loop only does so when `*s` lands on the terminator, which you then skip past with the post-increment. Therefore, the terminator is still there, before the concatenation sets in. Change it to `while (*s) ++s;` – WhozCraig Jan 06 '22 at 11:51

2 Answers2

2

This while loop within the function

while (*s++);

is incorrect. After the while loop the pointer s points to after the terminating zero character '\0' due to the postfix increment operator.

The function can be declared and defined the following way

char * strcat(char *s, const char *t)
{
    char *p = s;

    while( *p ) ++p;

    while ( ( *p++ = *t++ ) != '\0' );

    return s;
}

Also you should rename the function because there is already standard string function strcat in C.

Here is a demonstration program.

#include <stdio.h>

char *string_cat( char *s, const char *t )
{
    char *p = s;

    while (*p) ++p;

    while (( *p++ = *t++ ) != '\0');

    return s;
}

int main( void )
{
    char c[100] = "hello";
    const char *w = " world";

    puts( string_cat( c, w ) );
}

The program output is

hello world
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
1

In addition to @Vlad from Moscow good answer:

Local void strcat(char *s, char *t) assumes the strings referenced by s, t do not overlap in memory. Consider the infinite loop below if s == t.

char *p = s;
while (*p) p++;
while ((*p++ = *t++ ) != 0);

The standard C library has char *strcat(char * restrict s1, const char * restrict s2);. The restrict assumes that no-overlap and emits maybe better code> Otherwise the result in undefined behavior (UB). OP's code should also use restrict.

But what is we wanted to do strcat() and cope with overlap?

// Allow strings refenced by s,t to overlap.
char *my_strcat(char *s1, const char *s2) {
  size_t s1len = strlen(s1);
  size_t s2siz = strlen(s2) + 1u;
  memmove(s1 + s1len, s2, s2siz);
  return s1;
}

int main(void) {
  char c[100] = "hello";
  const char *w = c; //" world";
  puts(my_strcat(c, w));
}

Output

hellohello

To handle overlap, extra work occurred to determine the size of s2.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256