There is no string type; instead, strings are implemented as arrays of chars. You can’t simply interchange strings and chars in a line of code, as they are fundamentally different under the hood.
There is a version of strcpy that adds safety by passing in an N value that is the maximum size to copy, which is usually the size of the destination string.
Checkpoint2.6.6.Dynamic String Concatenation.
Put these lines of code in order to successfully create the string "Hello World" (held in result) after the code finishes.
char str1[10], str2[10], *result;
int len;
---
strcpy(str1, "Hello ");
strcpy(str2, , "World");
---
len = strlen(str1) + strlen(str2) + 1;
---
len = sizeof(str1) + sizeof(str2) + 1; #paired
---
len = sizeof(str1) + sizeof(str2); #paired
---
len = strlen(str1) + strlen(str2); #paired
---
result = (char*)malloc(len);
---
strcpy(result, str1);
---
strcat(result, str2);
---
printf("%s\n", result);
Think about whether you should do a string copy or a string concatenation first.
Checkpoint2.6.7.String overflow and undefined behavior.
Identify (by clicking) all of the lines that would result in string overflow or undefined behavior in the program below:
There are instances when strcat can be used safely. While strncat is a safer alternative to strcat, you should not become complacent. You can still easily buffer overflow if there’s not enough room in the destination string to copy the characters into.