提交记录 #356
提交时间:2024-12-02 17:36:29
语言:c
状态:Unaccepted
编译情况:编译成功
固定测试点#1:
固定测试点#2:
固定测试点#3:
附加测试点暂不可用
52【字符】字符替换*——用指针更方便
#include <stdio.h>
#include <string.h>
void replace(char *a, char *b) {
int l1 = strlen(a); // 源字符串的长度
int l2 = strlen(b); // 替换字符串的长度
// 结果字符串最大可以存储的字符数
char result[1000];
int resultIndex = 0; // 结果数组的当前索引
// 遍历源字符串,查找 'W' 并替换
for (int i = 0; i < l1; i++) {
if (a[i] == 'W') {
// 如果是 'W',则替换为字符串 b
for (int j = 0; j < l2; j++) {
result[resultIndex++] = b[j];
}
} else {
// 否则直接复制原字符
result[resultIndex++] = a[i];
}
}
result[resultIndex] = '\0'; // 确保字符串结尾
printf("%s\n", result); // 输出替换后的字符串
}
int main() {
char a[100], b[20];
// 输入源字符串和替换字符串
fgets(a, sizeof(a), stdin); // 输入源字符串
fgets(b, sizeof(b), stdin); // 输入替换字符串
// 去除输入的换行符
a[strcspn(a, "\n")] = '\0';
b[strcspn(b, "\n")] = '\0';
replace(a, b);
return 0;
}