提问者:小点点

如何在C++中不使用库替换字符


我有原型-int replace_char(string&char); 我不能从stringctype.h中使用库,我应该编写自己的函数。

所以任务是在文本中找到caharacter,我应该用“*”代替它。

示例:在This is my text中。 将所有T替换为*。 结果将是-*His is my*ex*

#include <iostream>
using namespace std;
int replace_char(string &, char);


int main ()
{
    cout << ""Please insert text:"
    cin >> str;
    
}

int replace_char(string str, char c1)
{
    for (int i = 0 ; i < str.length(); i++)
    {
        if(str[i]==c1)
            str[i]='*';
    }
    return str;
}

共3个答案

匿名用户

代码中有几个错误:

>

  • 函数签名不匹配,原型定义为std::string&,但在函数定义中仅使用了std::string

    在将每个字母与单个字符进行比较之前,程序从不转换大写字母T或任何大写字母。

    该函数从不在代码中使用。

    CIN>>; str不会在下一个空格字符后面加上较长的文本。

    函数希望返回一个整数,但实际返回的类型是std::string,这完全是一个误解。

    重新定义的代码:

    #include <iostream>
    
    // taking a reference of std::string and a char
    int replaceText(std::string&, char);
    
    int main(void) {
        std::string s;
        int rep;
    
        std::cout << "Enter a string: ";
        std::getline(std::cin, s); // getline() to accept whitespaces
    
        // since we're using a reference, the original variable is manipulated
        int rep = replaceText(s, 't');
    
        std::cout << "Output: " << s << std::endl;
        std::cout << "Replaced number of chars: " << rep << std::endl;
    
        return 0;
    }
    
    int replaceText(std::string& str, char c) {
        size_t len = str.length();
        static int count;
    
        // changing each letter into lowercase without using any built-in functions
        for (size_t i = 0; i < len; i++)
            if (str[i] >= 'A' && str[i] <= 'Z')
                str[i] = str[i] + 32;
    
        // replacing the character, the main work
        for (size_t i = 0; i < len; i++)
            if (str[i] == c) {
                str[i] = '*';
                count++; // count a static variable
            }
    
        return count; // returning the overall counts
    }
    

    该程序首先从std::string类型的用户获取输入,并使用对变量str的引用。 现在程序进入函数代码。

    在开始时,该函数将每个字母转换为小写,而不使用任何库或内置函数。 然后,它尝试仔细地比较字符串中的每个字母,一旦给定字符与传递给函数的字符串中包含的值匹配,它就替换并计数一个静态变量,该变量在整个程序生命周期中保留该值。

    然后,它简单地显示被操纵的字符串。

    它输出如下:

    Enter a string: This is a text
    Output: *his is a *ex*
    Replaced chars: 3
    

  • 匿名用户

    你似乎有一个很好的开始。

    您需要在将输入读入str之前声明str。 尝试string str;

    那么您需要在main中使用您的函数。 或者将其输出存储到另一个字符串中,如string replaced=replace_char(str,nt');

    或者像cout<<<; replace_char(str,nt')<<; endl;

    匿名用户

    也许这就是你需要的

    
    #include <iostream>
    using namespace std;
    int replace_char(string &, char);
    
    
    int main ()
    {
        string str;
    
        cout << "Please insert text:"
        std::getline(cin, str);
    
        int rlen = replace_text(str, 't')
    
        cout << str << endl;
        cout << "Number of replaced : " << rlen << endl;
     
        return 0;  
    }  
    
    int replace_char(string str, char c1)
    {
        int rlen = 0;
        for (int i = 0 ; i < str.length(); i++)
        {
            if(str[i]==c1) {
                str[i]='*';
                rlen++;
            }
        }
        return rlen;
    }
    
    

    相关问题


    MySQL Query : SELECT * FROM v9_ask_question WHERE 1=1 AND question regexp '(何在|c++|中|库|替换|字符)' ORDER BY qid DESC LIMIT 20
    MySQL Error : Got error 'repetition-operator operand invalid' from regexp
    MySQL Errno : 1139
    Message : Got error 'repetition-operator operand invalid' from regexp
    Need Help?