提交记录 #426
提交时间:2024-12-03 22:10:36
语言:c
状态:Unaccepted
编译情况:编译成功
code.c: In function ‘main’:
code.c:35:5: warning: implicit declaration of function ‘gets’; did you mean ‘fgets’? [-Wimplicit-function-declaration]
35 | gets(s);
| ^~~~
| fgets
/usr/bin/ld: /tmp/cccO8HsP.o: in function `main':
code.c:(.text.startup+0x21): warning: the `gets' function is dangerous and should not be used.
固定测试点#1:
固定测试点#2:
固定测试点#3:
额外测试点#7:
52【字符】字符替换*——用指针更方便
#include <stdio.h>
#include <string.h>
void replaceString(char *s, const char *t, const char *str) {
char result[100]; // 存储替换后的字符串
int s_len = strlen(s);
int t_len = strlen(t);
int str_len = strlen(str);
int result_index = 0;
for (int i = 0; i < s_len; ) {
// 检查当前位置是否是被替换串的开头
if (strncmp(&s[i], t, t_len) == 0) {
// 如果是,将替换串复制到结果字符串中
strcpy(&result[result_index], str);
result_index += str_len;
i += t_len; // 跳过被替换串
} else {
// 如果不是,将源串中的字符复制到结果字符串中
result[result_index++] = s[i++];
}
}
result[result_index] = '\0'; // 添加字符串结束符
strcpy(s, result); // 将结果字符串复制回源串
}
int main() {
char s[100]; // 源串
char t[10]; // 被替换串
char str[10]; // 替换串
// 输入源串、被替换串和替换串
gets(s);
gets(t);
gets(str);
// 替换字符串
replaceString(s, t, str);
// 输出替换后的字符串
printf("%s\n", s);
return 0;
}