如何知道字符串中的单词是否有数字?

时间:2015-04-25 21:56:23

标签: c++

我的问题:如何判断字符串中的单词是否包含数字?

我需要做的是:编写一个读取一行文本的程序,并输出所有整数中用'x'代替的所有数字的行。

例如:

  

我的用户ID是john17,我的4位数针是1234,这是秘密的。

它应该成为

  

我的用户ID是john17,我的x数字引脚是xxxx,这是秘密的。

注意john17中的数字不会受到影响。

到目前为止我的代码:

 #include <iostream>

 using namespace std;

 int main()
 {
    string str;
    int i;

    cout << "Enter a line of text: ";
    getline(cin, str);

    for (i = 0; i <= str.length(); i++)
    {
        if (str[i] >= '0' && str[i] <= '9')
        {
            str.at(i) = 'x';
        }
    }

    cout << "Edited text: " << str << endl;
}

6 个答案:

答案 0 :(得分:2)

您可以使用许多方法。以下是其中一些:

1 - 通用算法:

  1. 遍历字符串: 例如:"My userID is john17 and my 4 digit pin is 1234 which is secret."

  2. 当您在IsNumberOnly=true字符后面找到一个数字并开始缓存该单词时,设置一个布尔标志(space)。请注意,该标志最初为false。例如,示例中的第二个'1'是紧跟在space字符后面的数字。继续迭代,

  3. 如果您在到达另一个spaceIsNumberOnly=false之前找到了非数字,

  4. 完成这个词。如果IsNumberOnly=true然后打印'x' s(兑现字词中的字符数),则打印兑现字词,例如17John

  5. 请注意,此处john17根本不会受到影响。即使是17john它也不会受到影响。

    OR

    2 - 如果您想使用The Library和C ++ 11,那么您可以逐个阅读和测试单词:

    std::all_of(str.begin(), str.end(), ::isdigit); // C++11
    

    有关详细信息和示例:

    http://en.cppreference.com/w/cpp/algorithm/all_any_none_of

    OR

    3 - 这样的事情可以解决问题:

    #include <iostream>
    #include <sstream>
    using namespace std;
    
    int main()
    {
        string str;
        cout << "Enter a line of text: ";
        getline(cin,str);
    
        istringstream iss(str);
        string word;
        string finalStr="";
        while(iss >> word) {
            if(word.find_first_not_of( "0123456789" ) == string::npos)
            {
                for(int i=0; i<word.size(); i++)
                {
                    finalStr+='x';
                }
            }
            else
            {
                finalStr.append(word);
            }
            finalStr+=' ';
        }
    
        cout << "Edited text: " << finalStr << endl;
    
        return 0;
    }
    

    这些只是一些例子。

    希望有所帮助!

答案 1 :(得分:1)

您可以这样做:

对于您找到的任何数字,请检查它是否开始使用新词。

如果是,请替换直到数字序列结束。

如果名称不能以数字开头,它将起作用。

例如:

for( size_t i=str.find_first_of("0123456789"); i!=string::npos; i=str.find_first_of("0123456789",i+1)){ 
   if (i == 0 || str.at(i-1) == ' '){
      do{
          str.at(i) = 'x';
          i++;
      }while( isdigit( str.at(i)));
   }
}

答案 2 :(得分:1)

我可能会使用std::isdigit以及std::string input("My userID is john17 and my 4 digit pin is 1234 which is secret."); std::istringstream buffer(input); std::transform(std::istream_iterator<std::string>(buffer), std::istream_iterator<std::string>(), std::ostream_iterator<std::string>(std::cout, " "), [](std::string const &s) -> std::string { if (std::all_of(s.begin(), s.end(), isdigit)) return std::string(s.length(), 'x'); return s; }); ,类似这样的内容:

char

请注意,现在它具有未定义的行为:如果您的输入包含编码为isdigit时为负数的任何字符(例如,ISO 8859- *中带有重音符号,变音符号等的字符)然后unsigned char的行为未定义。在传递给isdigit之前,必须将每个字符强制转换为{{1}},以便在这种情况下给出定义的行为。

答案 3 :(得分:1)

与往常一样,STL提供了许多相对容易的工具。

这里我展示了如何使用流迭代器,all_of算法结合isdigit谓词。我的想法是将输入标记为单词,如果令牌的所有字符都是数字,则将其替换为x。

#include <iostream>
#include <iterator>
#include <algorithm>
#include <locale>

int main(int, char**)
{
    using iterator = std::istream_iterator<std::string>;
    auto const& end = iterator();

    for (auto it = iterator(std::cin); it != end; ++it) {
        auto const& token = *it;
        if (std::all_of(std::begin(token), std::end(token), [](auto&& c) { return std::isdigit(c, std::locale()); })) {
            std::cout << std::string(token.size(), 'x') << " ";
        } else {
            std::cout << token << " ";
        }
    }

    return 0;
}

你可以看到它live here

答案 4 :(得分:1)

使用find_first_offind_first_not_of的另一种选择。无论数字在哪里都可以工作:

#include <iostream>
#include <sstream>

int main()
{
   std::string str = "M3y u1serID is 7j2ohn17 and my 4 digit pin is 1234 which is secret.";
   std::istringstream iss(str);
   std::string word;
   while(iss >> word) 
   {
       if (word.find_first_not_of("0123456789") == std::string::npos) {
           std::cout << std::string(word.size(), 'x') << " ";
       } else {
           std::cout << word << " ";
       }
   }
}

答案 5 :(得分:0)

您的输入格式是否总是这样?

我的用户ID是john17,我的4位数针是1234,这是秘密的。

然后检查数字之前是否有空格。如果是,则用xxxxx更改号码,否则不用。

相关问题