便携以上借口的代码,源码供参考:#include <stdio.h> const int SIZE = 80; // 这两个函数由裁判程序提供 int pstr_scan(char* str, int size); void pstr_print(const char* str, int length); int pstr_cpy(char *s1, int len1, int size, const char *s2, int len2); int pstr_cat(char *s1, int len1, int size, const char *s2, int len2); int main() { char line[SIZE]; char text[SIZE/2]; int len1 = pstr_scan(line, SIZE); int len2 = pstr_scan(text, SIZE/2); len1 = pstr_cat(line, len1, SIZE, "\x0D\x0A", 2); len1 = pstr_cat(line, len1, SIZE, text, len2); len2 = pstr_cpy(text, len2, SIZE/2, line, len1); pstr_print(line, len1); pstr_print(text, len2); return 0; } /* 请在这里填写答案 */
时间: 2023-12-13 14:06:05 浏览: 26
以下是利用给定接口实现的代码:
```c
#include <stdio.h>
const int SIZE = 80;
int pstr_scan(char* str, int size);
void pstr_print(const char* str, int length);
int pstr_cpy(char *s1, int len1, int size, const char *s2, int len2);
int pstr_cat(char *s1, int len1, int size, const char *s2, int len2);
int main()
{
char line[SIZE];
char text[SIZE/2];
int len1 = pstr_scan(line, SIZE);
int len2 = pstr_scan(text, SIZE/2);
len1 = pstr_cat(line, len1, SIZE, "\x0D\x0A", 2);
len1 = pstr_cat(line, len1, SIZE, text, len2);
len2 = pstr_cpy(text, len2, SIZE/2, line, len1);
pstr_print(line, len1);
pstr_print(text, len2);
return 0;
}
int pstr_cpy(char *s1, int len1, int size, const char *s2, int len2)
{
if (len1 + len2 >= size) {
return -1; // 字符数组容量不足,拷贝失败
}
int i;
for (i = 0; i < len2; i++) {
s1[i] = s2[i];
}
s1[i] = '\0'; // 添加字符串结束符
return i;
}
int pstr_cat(char *s1, int len1, int size, const char *s2, int len2)
{
if (len1 + len2 >= size) {
return -1; // 字符数组容量不足,拼接失败
}
int i;
for (i = 0; i < len2; i++) {
s1[len1 + i] = s2[i];
}
s1[len1 + i] = '\0'; // 添加字符串结束符
return len1 + i;
}
```
这段代码实现了在输入字符串 `line` 和 `text` 后,将 `text` 拼接到 `line` 的末尾,并将拼接后的结果存储在 `line` 中。然后,将 `line` 和 `text` 分别打印出来。注意,这段代码的 `pstr_cpy` 和 `pstr_cat` 函数的实现是假设输入参数有效的简化版本,实际应用中可能需要进行更多的参数检查和错误处理。