-
Use the gets() function cautiously. The gets() function is not very secure. It does not check the length of the input string and may cause a buffer overflow. You can use the fgets() function instead of
-
Your Strcat input string is best to use const char. You don't change the input string.
-
Boundary handling problem
#include
#include
#include
void Strcat(char* dest, const char* src);
int main()
{
char str1[200];
char str2[100];
fgets(str1, sizeof(str1), stdin);
fgets(str2, sizeof(str2), stdin);
Strcat(str1, str2);
printf("%s", str1);
return 0;
}
void Strcat(char* dest, const char* src)
{
// 将指针移动到目标字符串的末尾
while (*dest != '\0')
{
dest++;
}
// 将源字符串复制到目标字符串中
while (*src != '\0')
{
// 检查缓冲区溢出
if (dest >= (dest + sizeof(dest) - strlen(src) - 1))
{
fprintf(stderr, "Error: Destination string overflow\n");
exit(EXIT_FAILURE);
}
*dest = *src;
dest++;
src++;
}
// 在字符串末尾添加空字符
*dest = '\0';
}