How do I know if a word has numbers in a string?

The question I have is: how can you determine if a word has a number in a string or not?

What I need to do: write a program that reads in a line of text and prints a line with all the digits in all integers replaced by "x".

For instance:

My userID is john17 and my 4 digit output is 1234 which is secret.

He must become

My userID is john17, and my x-valued pin is xxxx, which is secret.

Note that the numbers in john17 are not affected.

The code I have so far is:

 #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;
}
+4
source share
6 answers

There are many methods that you can use. Below are some of them:

1 - General algorithm:

  • : : "My userID is john17 and my 4 digit pin is 1234 which is secret."

  • (IsNumberOnly=true), space . , false . , '1' space. ,

  • space IsNumberOnly=false,

  • . IsNumberOnly=true, 'x' ( , ), , 17John.

, john17 . 17John, .

2 - The Library ++ 11,

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

:

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

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;
}

.

, !

+2

- :

, , .

, .

, .

:

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)));
   }
}
+1

std::all_of 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;
    });

, undefined: , char (, , .. ISO 8859- *) isdigit undefined. unsigned char isdigit, .

+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;
}

And you can see live here .

+1
source

Another alternative using find_first_ofand find_first_not_of. Will work no matter where the numbers are in the word:

#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 << " ";
       }
   }
}
+1
source

Does your input format always look like this?

My userID is john17 and my 4 digit output is 1234 which is secret.

Then just check if there is a space before the numbers or not. If so, change the number from xxxxx else not.

0
source

Source: https://habr.com/ru/post/1584673/


All Articles