3 Ways to stitch the "C Language Learning Notes" string.

Source: Internet
Author: User

Last night and @buptpatriot discussed the question of the function return pointer (malloc generated), referring to string concatenation, to make a summary.

[CPP]View Plaincopyprint?
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. Char *join1 (char *, char*);
  5. void Join2 (char *, char *);
  6. Char *join3 (char *, char*);
  7. int main (void) {
  8. char a[4] = "abc"; //char *a = "abc"
  9. char b[4] = "Def"; //Char *b = "def"
  10. char *c = Join3 (A, b);
  11. printf ("concatenated String is%s\n", c);
  12. Free (c);
  13. c = NULL;
  14. return 0;
  15. }
  16. /* Method One, do not change the string A, B, by malloc, generate a third string C, return the local pointer variable */
  17. Char *join1 (char *a, char *b) {
  18. char *c = (char *) malloc (strlen (a) + strlen (b) + 1); //local variables, requesting memory with malloc
  19. if (c = = NULL) exit (1);
  20. char *TEMPC = c; //Save the first address
  21. While (*a! = ')} {
  22. *c++ = *a++;
  23. }
  24. While ((*c++ = *b++)! = ' + ' ) {
  25. ;
  26. }
  27. //Note that at this point the pointer C has pointed to the end of the string after stitching!
  28. return TEMPC; The //return value is a pointer variable for the local malloc request, which is required after the function call ends
  29. }
  30. /* Method Two, get rid of the string directly a,*/
  31. void Join2 (char *a, char *b) {
  32. //Note that if a A, B in the main function defines a string constant (as follows):
  33. //char *a = "abc";
  34. //char *b = "Def";
  35. //Then the join2 is not feasible.
  36. //must be defined like this:
  37. //char a[4] = "ABC";
  38. //char B[4] = "Def";
  39. While (*a! = ')} {
  40. a++;
  41. }
  42. While ((*a++ = *b++)! = ' + ' ) {
  43. ;
  44. }
  45. }
  46. /* Method Three, call C library function, */
  47. char* join3 (char *s1, char *s2)
  48. {
  49. char *result = malloc (strlen (S1) +strlen (S2) +1); //+1 for the Zero-terminator
  50. //in Real code would check for errors on malloc here
  51. if (result = = NULL) exit (1);
  52. strcpy (result, S1);
  53. strcat (result, S2);
  54. return result;
  55. }

3 Ways to stitch the "C Language Learning Notes" string.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.