关于#c++#的问题,如何解决?(语言-c++)

题目描述
把一个字符串中特定的字符全部用给定的字符替换,得到一个新的字符串。
输入
只有一行,由一个字符串和两个字符组成,中间用单个空格隔开。字符串是待替换的字符串,字符串长度小于等于30 个字符,且不含空格等空白符;
接下来一个字符为需要被替换的特定字符;
接下来一个字符为用于替换的给定字符。
输出
一行,即替换后的字符串。
样例输入
hello-how-are-you o O
样例输出
hellO-hOw-are-yOu

代码:

#include<bits/stdc++.h>
using namespace std;
int main()
{
    string a,b,c;
    cin>>a>>b>>c;
    for(int i=0;i<a.size();i++)
    {
        if(a[i]==b[0]) a[i]=c[0];
     } 
    for(int i=0;i<a.size();i++)
    {
        cout<<a[i]<<endl; 
     } 
}
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char s[31];
char ch1, ch2;
cin >> s;
cin >> ch1 >> ch2;
for (int i = 0; s[i]; i++)
if (s[i] == ch1) s[i] = ch2;
cout << s;
return 0;
}

完整代码加注释

#include <iostream>
#include <string>
using namespace std;

int main() {
  string s; // 待替换的字符串
  char c1, c2; // 需要被替换的字符和用于替换的字符
  cin >> s >> c1 >> c2; // 输入字符串和两个字符
  for (int i = 0; i < s.length(); i++) { // 遍历字符串中的每个字符
    if (s[i] == c1) { // 如果当前字符是需要被替换的字符
      s[i] = c2; // 用给定的字符替换它
    }
  }
  cout << s << endl; // 输出替换后的字符串
  return 0;
}



#include<cstdio>
#include<cstring>

void repl(char* old,char y,char h,int len){
    char result[len] ;
    strcpy(result,old);
    for (int i = 0; result[i]; i++)
        if (result[i] == y) result[i] = h;
    printf("%s",result);
}
int main()
{
    char old[] = "hello-how-are-you";
    char y = 'o';
    char h = 'O';    
    repl(old,y,h,18);
    return 0;
}
不知道你这个问题是否已经解决, 如果还没有解决的话:
  • 你可以看下这个问题的回答https://ask.csdn.net/questions/7621763
  • 你也可以参考下这篇文章:输入一个字符串,以#结尾,判断字符串中连续数字的长度,并输出这个数字字符串
  • 以下回答由chatgpt基于相关博客总结生成:

    在C++中解决这个问题可以通过遍历字符串的每个字符,将特定字符替换为给定字符,然后将替换后的字符串进行输出。

    以下是解决该问题的具体步骤和示例代码:

    1. 读取输入的字符串、需要替换的特定字符和替换的给定字符。
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main() {
        string str;
        char oldChar, newChar;
        cin >> str >> oldChar >> newChar;
        // 输入示例 hello-how-are-you o O
    
        // 进行字符替换的代码
        // ...
    
        return 0;
    }
    
    1. 遍历字符串的每个字符,如果当前字符与需要替换的特定字符相同,则将其替换为给定字符。
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main() {
        string str;
        char oldChar, newChar;
        cin >> str >> oldChar >> newChar;
        // 输入示例 hello-how-are-you o O
    
        // 进行字符替换的代码
        for (int i = 0; i < str.length(); i++) {
            if (str[i] == oldChar) {
                str[i] = newChar;
            }
        }
    
        cout << str << endl;  // 输出替换后的字符串 hello-hOw-are-yOu
    
        return 0;
    }
    
    1. 将替换后的字符串输出。
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main() {
        string str;
        char oldChar, newChar;
        cin >> str >> oldChar >> newChar;
        // 输入示例 hello-how-are-you o O
    
        // 进行字符替换的代码
        for (int i = 0; i < str.length(); i++) {
            if (str[i] == oldChar) {
                str[i] = newChar;
            }
        }
    
        cout << str << endl;  // 输出替换后的字符串 hello-hOw-are-yOu
    
        return 0;
    }
    

    以上是使用C++解决该问题的具体步骤和示例代码。


如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^