fork download
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. #define SIZE 100
  5.  
  6. void copyUsingArray(char destination[],
  7. const char source[])
  8. {
  9. int i = 0;
  10.  
  11. while ((destination[i] = source[i]) != '\0')
  12. {
  13. ++i;
  14. }
  15. }
  16.  
  17. void copyUsingPointer(char *destination,
  18. const char *source)
  19. {
  20. while ((*destination = *source) != '\0')
  21. {
  22. ++destination;
  23. ++source;
  24. }
  25. }
  26.  
  27. void concatenateUsingArray(char destination[],
  28. const char source[])
  29. {
  30. int i = 0;
  31. int j = 0;
  32.  
  33. while (destination[i] != '\0')
  34. {
  35. ++i;
  36. }
  37.  
  38. while ((destination[i] = source[j]) != '\0')
  39. {
  40. ++i;
  41. ++j;
  42. }
  43. }
  44.  
  45. void concatenateUsingPointer(char *destination,
  46. const char *source)
  47. {
  48. while (*destination != '\0')
  49. {
  50. ++destination;
  51. }
  52.  
  53. while ((*destination = *source) != '\0')
  54. {
  55. ++destination;
  56. ++source;
  57. }
  58. }
  59.  
  60. int main(void)
  61. {
  62. char source[] = "Hello World";
  63. char destination1[SIZE] = "";
  64. char destination2[SIZE] = "";
  65.  
  66. copyUsingArray(destination1, source);
  67. copyUsingPointer(destination2, source);
  68.  
  69. printf("Copy using array notation: %s\n",
  70. destination1);
  71.  
  72. printf("Copy using pointer notation: %s\n",
  73. destination2);
  74.  
  75. char first1[SIZE] = "Good ";
  76. char first2[SIZE] = "Good ";
  77. char second[] = "Morning";
  78.  
  79. concatenateUsingArray(first1, second);
  80. concatenateUsingPointer(first2, second);
  81.  
  82. printf("Concatenate using array notation: %s\n",
  83. first1);
  84.  
  85. printf("Concatenate using pointer notation: %s\n",
  86. first2);
  87.  
  88. return 0;
  89. }
Success #stdin #stdout 0s 5340KB
stdin
Standard input is empty
stdout
Copy using array notation: Hello World
Copy using pointer notation: Hello World
Concatenate using array notation: Good Morning
Concatenate using pointer notation: Good Morning