识别整数并用 X 替换数字

问题描述

所以,我正在开发一个程序,用 x 替换输入 cin 的每个数字,但如果它是类似 john17 的东西,那么不要(所以基本上,如果单词是整数,则替换数字,否则,保留字原样)。我正在努力在这里找到解决方案。如何将输入的行中的单词拆分并测试它是否为整数?

所以要添加一些说明……这是一行文本,例如:“我的用户 ID 是 john17,我的 4 位密码是 1234,这是秘密”,所以我想将 john17 保留在那里,但将 4 替换为 x,将 1234 替换为所有 x

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

// Our variables
string str;
string result;

//Function Prototypes
void replace_num_with_x(string str);

int main() {

    cout<<"Please enter a line of text:";
    getline(cin,str);
    replace_num_with_x(str);
    cout<<result;

}

//Function DeFinition
void replace_num_with_x(string str) {
    stringstream str_strm;
    str_strm << str; //Convert string to stringstream

    for(int i = 0; i <= str.length() ; i++) {
        if((char)str[i] >=48 && (char)str[i] <= 57) {
            result +='x';
        }
        else {
            result +=str[i];
        }
    }
}

解决方法

你离得不远了。 replace_num_with_x() 正在创建一个未在任何地方使用的 stringstream。更改循环以从该流中读取单个单词,例如:

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

string replace_num_with_x(const string &str);

int main() {
    cout << "Please enter a line of text:";
    string str;
    getline(cin,str);
    cout << replace_num_with_x(str);
}

string replace_num_with_x(const string &str) {
    istringstream str_strm(str);
    string word,result;

    while (str_strm >> word) {
        bool all_digits = true;
        string::size_type word_size = word.size();

        for (string::size_type i = 0; i < word_size; ++i) {
            if (word[i] < '0' || word[i] > '9') {
                all_digits = false;
                break;
            }
        }

        if (!result.empty()) {
            result += ' ';
        }

        if (all_digits) {
            result.append(word_size,'x');
        } else {
            result += word;
        }
    }
    
    return result;
}

read more about these functions here

或者:

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

string replace_num_with_x(const string &str);

int main() {
    cout << "Please enter a line of text:";
    string str;
    getline(cin,str);
    cout << replace_num_with_x(str);
}

string replace_num_with_x(const string &str) {
    istringstream str_strm(str);
    ostringstream out_strm;
    string word;

    if (str_strm >> word) {
        do {
            if (word.find_first_not_of("0123456789") == string::npos) {
                fill(word.begin(),word.end(),'x');
            }
            out_strm << word;
            if (!(str_strm >> word)) break;
            out_strm << ' ';
        }
        while (true);
    }

    return out_strm.str();
}

Demo


UPDATE:如果您不被允许使用 istringstream,那么您可以做更多类似的事情:

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

string replace_num_with_x(const string &str);

int main() {
    cout << "Please enter a line of text:";
    string str;
    getline(cin,str);
    cout << replace_num_with_x(str);
}

string replace_num_with_x(const string &str) {
    string word,result;
    string::size_type str_size = str.size(),start = 0,end;

    do {
        end = str.find(' ',start);
        if (end == string::npos) {
            word = str.substr(start);
            start = str_size;
        }
        else {
            word = str.substr(start,end - start);
            start = end + 1;
        }

        bool all_digits = true;
        string::size_type word_size = word.size();

        for (string::size_type i = 0; i < word_size; ++i) {
            if (word[i] < '0' || word[i] > '9') {
                all_digits = false;
                break;
            }
        }

        if (!result.empty()) {
            result += ' ';
        }

        if (all_digits) {
            result.append(word_size,'x');
        } else {
            result += word;
        }
    }
    while (start < str_size);

    return result;
}

Demo