当前位置:网站首页>Openjudge noi 1.7 08: character substitution

Openjudge noi 1.7 08: character substitution

2022-07-07 01:02:00 Jun Yi_ noip

【 Topic link 】

OpenJudge NOI 1.7 08: Character substitution

【 Topic test site 】

1. character string

【 Solution code 】

solution 1: Use character array

#include <bits/stdc++.h>
using namespace std;
int main()
{
    
    char s[35], a, b;// character a Replace with b
    cin >> s >> a >> b;
    int len = strlen(s);
    for(int i = 0; i < len; i++)
    {
    
        if(s[i] == a)
            s[i] = b;
    }
    cout << s;
    return 0;
}

solution 2: Use string class

#include <bits/stdc++.h>
using namespace std;
int main()
{
    
    string s;
    char a, b;// character a Replace with b
    cin >> s >> a >> b;
    for(int i = 0; i < s.length(); i++)
    {
    
        if(s[i] == a)
            s[i] = b;
    }
    cout << s;
    return 0;
}
原网站

版权声明
本文为[Jun Yi_ noip]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202130937380233.html