-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathC10_Copy_string_program.c
executable file
·127 lines (92 loc) · 3.81 KB
/
C10_Copy_string_program.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
* @Author: Daniel Maurelle
* @Email: daniel.maurelle@gmail.com
* Youtube: https://youtube.com/playlist?list=PLDqCEJxpkYKCzZSwAz-2PpzcwMDY11NGp&si=HRSvwoUxsEBtFsfI
* @Date: 12-18-2023 18:20:55
* @Description: 10-Copy string program.
*
* MIT License
*
* Copyright 2023 Daniel Maurelle
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <stdio.h>
void stringCopy(char *dest, const char *src) {
while (*src) {
*dest++ = *src++;
}
*dest = '\0'; // Null-terminate the destination string
}
int main() {
char source[] = "Hello, Embedded World!";
char destination[30]; // Ensure this is large enough to hold the source string
stringCopy(destination, source);
printf("Copied String: %s\n", destination);
return 0;
}
/*
In this program:
The stringCopy function takes two pointers: dest (destination) and src (source). It copies each character
from the source to the destination, incrementing both pointers until the end of the source string is reached,
indicated by the null character '\0'.
The main function demonstrates the usage of stringCopy. It defines a source string and a destination array (sized to ensure
it can hold the source string).
The copied string is then printed to the standard output.
*/
/*
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void stringCopy(char *dest, const char *src) {
while (*src) {
*dest++ = *src++;
}
*dest = '\0'; // Null-terminate the destination string
}
int main() {
char source[] = "Hello, Embedded World!";
char *destination;
// Allocate memory for the destination based on the size of the source
destination = (char *)malloc(strlen(source) + 1); // +1 for the null terminator
if (destination == NULL) {
fprintf(stderr, "Memory allocation failed\n");
return 1;
}
stringCopy(destination, source);
printf("Copied String: %s\n", destination);
// Free the allocated memory
free(destination);
return 0;
}
*/
/*
Changes made:
Included <stdlib.h> and <string.h> for malloc, free, and strlen.
Allocated memory for destination using malloc, ensuring it's large enough to hold the source string plus the null terminator.
Added a check to ensure malloc successfully allocated memory.
Used free to release the memory allocated to destination at the end of main.
Note:
Memory allocation with malloc can fail, so it's important to check if the returned pointer is NULL.
Always free dynamically allocated memory when it's no longer needed to prevent memory leaks.
This approach assumes that your embedded system supports dynamic memory allocation, which is not always the case in
constrained environments.
If dynamic allocation is not supported, you would need to declare destination with a fixed size, ensuring it's large enough
for any expected input.
*/