Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
60
Task/Copy-a-string/C/copy-a-string-1.c
Normal file
60
Task/Copy-a-string/C/copy-a-string-1.c
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
#include <stdlib.h> /* exit(), free() */
|
||||
#include <stdio.h> /* fputs(), perror(), printf() */
|
||||
#include <string.h>
|
||||
|
||||
int
|
||||
main()
|
||||
{
|
||||
size_t len;
|
||||
char src[] = "Hello";
|
||||
char dst1[80], dst2[80];
|
||||
char *dst3, *ref;
|
||||
|
||||
/*
|
||||
* Option 1. Use strcpy() from <string.h>.
|
||||
*
|
||||
* DANGER! strcpy() can overflow the destination buffer.
|
||||
* strcpy() is only safe if the source string is shorter than
|
||||
* the destination buffer. We know that "Hello" (6 characters
|
||||
* with the final '\0') easily fits in dst1 (80 characters).
|
||||
*/
|
||||
strcpy(dst1, src);
|
||||
|
||||
/*
|
||||
* Option 2. Use strlen() and memcpy() from <string.h>, to copy
|
||||
* strlen(src) + 1 bytes including the final '\0'.
|
||||
*/
|
||||
len = strlen(src);
|
||||
if (len >= sizeof dst2) {
|
||||
fputs("The buffer is too small!\n", stderr);
|
||||
exit(1);
|
||||
}
|
||||
memcpy(dst2, src, len + 1);
|
||||
|
||||
/*
|
||||
* Option 3. Use strdup() from <string.h>, to allocate a copy.
|
||||
*/
|
||||
dst3 = strdup(src);
|
||||
if (dst3 == NULL) {
|
||||
/* Failed to allocate memory! */
|
||||
perror("strdup");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
/* Create another reference to the source string. */
|
||||
ref = src;
|
||||
|
||||
/* Modify the source string, not its copies. */
|
||||
memset(src, '-', 5);
|
||||
|
||||
printf(" src: %s\n", src); /* src: ----- */
|
||||
printf("dst1: %s\n", dst1); /* dst1: Hello */
|
||||
printf("dst2: %s\n", dst2); /* dst2: Hello */
|
||||
printf("dst3: %s\n", dst3); /* dst3: Hello */
|
||||
printf(" ref: %s\n", ref); /* ref: ----- */
|
||||
|
||||
/* Free memory from strdup(). */
|
||||
free(dst3);
|
||||
|
||||
return 0;
|
||||
}
|
||||
22
Task/Copy-a-string/C/copy-a-string-2.c
Normal file
22
Task/Copy-a-string/C/copy-a-string-2.c
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
#include <stdlib.h> /* exit() */
|
||||
#include <stdio.h> /* fputs(), printf() */
|
||||
#include <string.h>
|
||||
|
||||
int
|
||||
main()
|
||||
{
|
||||
char src[] = "Hello";
|
||||
char dst[80];
|
||||
|
||||
/* Use strlcpy() from <string.h>. */
|
||||
if (strlcpy(dst, src, sizeof dst) >= sizeof dst) {
|
||||
fputs("The buffer is too small!\n", stderr);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
memset(src, '-', 5);
|
||||
printf("src: %s\n", src); /* src: ----- */
|
||||
printf("dst: %s\n", dst); /* dst: Hello */
|
||||
|
||||
return 0;
|
||||
}
|
||||
23
Task/Copy-a-string/C/copy-a-string-3.c
Normal file
23
Task/Copy-a-string/C/copy-a-string-3.c
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
#include <gadget/gadget.h>
|
||||
|
||||
LIB_GADGET_START
|
||||
|
||||
Main
|
||||
String v, w = "this message is a message";
|
||||
|
||||
Let( v, "Hello world!");
|
||||
Print "v = %s\nw = %s\n\n", v,w;
|
||||
|
||||
Get_fn_let( v, Upper(w) );
|
||||
|
||||
Print "v = %s\nw = %s\n\n", v,w;
|
||||
|
||||
Stack{
|
||||
Store ( v, Str_tran_last( Upper(w), "MESSAGE", "PROOF" ) );
|
||||
}Stack_off;
|
||||
|
||||
Print "v = %s\nw = %s\n\n", v,w;
|
||||
|
||||
Free secure v, w;
|
||||
|
||||
End
|
||||
Loading…
Add table
Add a link
Reference in a new issue