Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,857 questions

51,778 answers

573 users

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 86 views
86 views asked Dec 15, 2022 by avibootz
1 answer 142 views
1 answer 103 views
103 views asked Dec 7, 2022 by avibootz
...