Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function to check if string contains a number

I'm working on a project in c++ (which I just started learning) and can't understand why this function is not working. I'm attempting to write a "Person" class with a variable first_name, and use a function set_first_name to set the name. Set_first_name needs to call a function(the one below) to check if the name has any numbers in it. The function always returns false, and I'm wondering why? Also, is this the best way to check for numbers, or is there a better way?

   bool Person::contains_number(std::string c){ // checks if a string contains a number
        if (c.find('0') == std::string::npos || c.find('1') == std::string::npos || c.find('2') == std::string::npos || c.find('3') == std::string::npos
        || c.find('4') == std::string::npos || c.find('5') == std::string::npos || c.find('6') == std::string::npos || c.find('7') == std::string::npos
        || c.find('8') == std::string::npos || c.find('9') == std::string::npos){// checks if it contains number

        return false;
        }
        return true;
    }
like image 495
sinθ Avatar asked Mar 09 '12 23:03

sinθ


People also ask

How do you check if a string includes a number?

To find whether a given string contains a number, convert it to a character array and find whether each character in the array is a digit using the isDigit() method of the Character class.

How do you check if a string has a number in it in Python?

str. isdigit() returns True if all the characters in the given string are digits, False otherwise. If stringExample contains at least a number, then the above code returns True because chr. isdigit() for chr in stringExample has at least one True in the iterable.

Which function is used to check whether a string contains a digit or not?

The str. isnumeric() checks whether all the characters of the string are numeric characters or not. It will return True if all characters are numeric and will return False even if one character is non-numeric.

How do you check if input contains numbers?

The isNan() function in javascript determines if the input passed is a number or not. This function returns true if the parameter is Not a Number. Else returns false.


5 Answers

Change all your || to &&.

Better yet:

return std::find_if(s.begin(), s.end(), ::isdigit) != s.end();        

Or, if you have it:

return std::any_of(s.begin(), s.end(), ::isdigit);
like image 131
Benjamin Lindley Avatar answered Oct 10 '22 06:10

Benjamin Lindley


C++11:

#include <algorithm>
#include <cctype>
#include <string>
#include <iostream>

bool has_any_digits(const std::string& s)
{
    return std::any_of(s.begin(), s.end(), ::isdigit);
}

int main()
{
    std::string query("H311o, W0r1d!");

    std::cout << query << ": has digits: "
              << std::boolalpha
              << has_any_digits(query)
              << std::endl;
    return 1;
}

Output:

H311o, W0r1d!: has digits: true

like image 27
Peter Wood Avatar answered Oct 10 '22 04:10

Peter Wood


It always returns false because your logic is backwards. You are using the || operator with == npos checks. If any one particular digit is missing from the string, == npos evaluates to true and || is satisfied, so you return false. You need to using != npos checks and then return true if any check evaluates to true:

bool Person::contains_number(const std::string &c)
{
    if (c.find('0') != std::string::npos ||
        c.find('1') != std::string::npos ||
        c.find('2') != std::string::npos ||
        c.find('3') != std::string::npos ||
        c.find('4') != std::string::npos ||
        c.find('5') != std::string::npos ||
        c.find('6') != std::string::npos ||
        c.find('7') != std::string::npos ||
        c.find('8') != std::string::npos ||
        c.find('9') != std::string::npos)
    {
        return true;
    }

    return false;
}

Or:

bool Person::contains_number(const std::string &c)
{
    return (
        c.find('0') != std::string::npos ||
        c.find('1') != std::string::npos ||
        c.find('2') != std::string::npos ||
        c.find('3') != std::string::npos ||
        c.find('4') != std::string::npos ||
        c.find('5') != std::string::npos ||
        c.find('6') != std::string::npos ||
        c.find('7') != std::string::npos ||
        c.find('8') != std::string::npos ||
        c.find('9') != std::string::npos
    );
}

A simplier solution is to use find_first_of() instead of find():

bool Person::contains_number(const std::string &c)
{
    return (c.find_first_of("0123456789") != std::string::npos);
}    
like image 21
Remy Lebeau Avatar answered Oct 10 '22 06:10

Remy Lebeau


How to test if a string contains any digits in C++

This should do it!

if (std::string::npos != s.find_first_of("0123456789"))
{
  std::cout << "digit(s)found!" << std::endl;
}
like image 30
prelic Avatar answered Oct 10 '22 06:10

prelic


You are using || (or operator) to check several conditions in an if statement. The or operator returns true (satisfies the condition) if one of the expression is true.

The or operator evaluates first the expression on its left: if that is true then it doesn't evaluate the expression on its right and returns true. If the expression on the left is false then the expression on the right is evaluated and the result of it is returned as result of || operator

This is what happen in your function:

  • does c contains '0' ? if no (because std::string::npos in find() means not found) then return false
  • does c contains '1' ? if no then return false
  • ...

So, replace the or operators with && (and operator).

like image 36
Paolo Brandoli Avatar answered Oct 10 '22 04:10

Paolo Brandoli