How to implement the function strcat() from string.h to append a string to other string in C

2 Answers

0 votes
#include <stdio.h>

#define LEN 30
 
void my_strcat(char dest[], char src[]);
  
int main(void)
{
    char s1[LEN] = "the new c", s2[] = " ide";
 
    my_strcat(s1, s2);
    puts(s1);
    
    return 0;
}
 
void my_strcat(char dest[], char src[]) 
{ 
    int i = 0, j = 0; 

    while (dest[i] != '\0') i++; // find the end of dest
       
    while ( (dest[i++] = src[j++]) != '\0'); // copy src to dest
} 
  

/*
   
run:
   
the new c ide

*/

 



answered Nov 10, 2015 by avibootz
0 votes
#include <stdio.h>

#define LEN 30
 
char *my_strcat(char *dest, char *src);
  
int main(void)
{
    char s1[LEN] = "the new c", s2[] = " ide", *p;
 
    p = my_strcat(s1, s2);
    puts(p);
    
    return 0;
}
 
char *my_strcat(char *dest, char *src) 
{ 
    int i = 0, j = 0; 

    while (dest[i] != '\0') i++; // find the end of dest
       
    while ( (dest[i++] = src[j++]) != '\0'); // copy src to dest
    
    return dest;
} 
  

/*
   
run:
   
the new c ide

*/

 



answered Nov 10, 2015 by avibootz

Related questions

1 answer 74 views
74 views asked Dec 15, 2022 by avibootz
1 answer 126 views
1 answer 90 views
90 views asked Dec 7, 2022 by avibootz
...