题解 | 字符串操作

字符串操作

https://www.nowcoder.com/practice/06a5336b64e3481fbbcc1f7d5cba548d

#include <stdio.h>

#include <string.h>

int main() {
    int n, m;
    scanf("%d %d", &n, &m);

    char s[101]; // 字符串长度不超过100,所以数组大小设为101(包含'\0')
    scanf("%s", s);

    for (int i = 0; i < m; i++) {
        int l, r;
        char c1, c2;
        scanf("%d %d %c %c", &l, &r, &c1, &c2);

        // 注意:题目中区间是1-based,转换为字符串的0-based索引
        for (int j = l - 1; j <= r - 1; j++) {
            if (s[j] == c1) {
                s[j] = c2;
            }
        }
    }

    printf("%s\n", s);
    return 0;
}

算法学习篇:新手入门130 文章被收录于专栏

算法学习篇:新手入门130

全部评论

相关推荐

迷茫的大四🐶:只有花了钱才能吃一堑长一智
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务