FIFA-2022 Career Guide Free Tutorials Go to Your University Placement Preparation 
0 like 0 dislike
265 views
in Tutorial & Interview questions by Goeduhub's Expert (9.3k points)

 Copy and Concatenation: strcpy(), strcat()

1 Answer

0 like 0 dislike
by Goeduhub's Expert (9.3k points)
 
Best answer

#include <stdio.h> 

#include <string.h>

int main(void) 

{                   /* Always ensure that your string is large enough to contain the characters   * and a terminating NUL character ('\0')!   */  

char mystring[10];

  /* Copy "foo" into `mystring`, until a NUL character is encountered. */  

strcpy(mystring, "foo");  

printf("%s\n", mystring);

  /* At this point, we used 4 chars of `mystring`, the 3 characters of "foo",   * and the NUL terminating byte.   */

  /* Append "bar" to `mystring`. */  

strcat(mystring, "bar");  

printf("%s\n", mystring);

  /* We now use 7 characters of `mystring`: "foo" requires 3, "bar" requires 3   * and there is a terminating NUL character ('\0') at the end.   */

  /* Copy "bar" into `mystring`, overwriting the former contents. */  

strcpy(mystring, "bar");  

printf("%s\n", mystring);

return 0; 

}

Outputs:

foo foobar bar

If you append to or from or copy from an existing string, ensure it is NUL-terminated!

String literals (e.g. "foo") will always be NUL-terminated by the compiler. 

Learn & Improve In-Demand Data Skills Online in this Summer With  These High Quality Courses[Recommended by GOEDUHUB]:-

Best Data Science Online Courses[Lists] on:-

Claim your 10 Days FREE Trial for Pluralsight.

Best Data Science Courses on Datacamp
Best Data Science Courses on Coursera
Best Data Science Courses on Udemy
Best Data Science Courses on Pluralsight
Best Data Science Courses & Microdegrees on Udacity
Best Artificial Intelligence[AI] Courses on Coursera
Best Machine Learning[ML] Courses on Coursera
Best Python Programming Courses on Coursera
Best Artificial Intelligence[AI] Courses on Udemy
Best Python Programming Courses on Udemy

 Important Lists:

Important Lists, Exams & Cutoffs Exams after Graduation PSUs

 Goeduhub:

About Us | Contact Us || Terms & Conditions | Privacy Policy ||  Youtube Channel || Telegram Channel © goeduhub.com Social::   |  | 

 

Free Online Directory

...