CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13
  1. #1
    Join Date
    May 2003
    Location
    San Antonio TX
    Posts
    380

    std::string all numeric

    What are some ways to determine if a std::string contains all numeric data? (Alpha representations of numeric data)

    Code:
    std::string sPrimaryNumber = "98";
    
    sPrimaryNumber.find_first_not_of("0123456789");
    John 3:16
    For God so loved the world ...

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: std::string all numeric

    That would do it....what else are you asking for?

    There may be other calls you could make, but the basic algorithm isn't going to be any different in most cases. The complexity of that algorithm is going to be O(n), and you can't really do better than that.

  3. #3
    Join Date
    May 2009
    Posts
    2,413

    Re: std::string all numeric

    Quote Originally Posted by kenrus View Post
    What are some ways to determine if a std::string contains all numeric data? (Alpha representations of numeric data)
    It's not that easy to say because what's numeric data is something you determine yourself.

    So what do you consider to be numeric data?

  4. #4
    Join Date
    Sep 2009
    Posts
    57

    Re: std::string all numeric

    Code:
    bool check_string_numeric(std::string str)
    {
        for(int i = 0; i < str.length(); i++)
        {
            if(!( (str.c_str()[i] >= '0') && (str.c_str()[i] <= '9') ) )
            {
                //Not numeric
                return false;
            }
        }
        //numeric
        return true;
    }

  5. #5
    Join Date
    Oct 2009
    Posts
    2

    Re: std::string all numeric

    Hi, folks

    I'm new to this forum... sorry for barging in here, but I cannot seem to figure out how to start a thread on this forum! Can you please give me a clue!

    Best regards and please forgive the intrusion ...

    DKean

  6. #6
    Join Date
    Apr 1999
    Posts
    27,449

    Re: std::string all numeric

    Quote Originally Posted by salehhamadeh View Post
    Code:
    bool check_string_numeric(std::string str)
    {
        for(int i = 0; i < str.length(); i++)
        {
            if(!( (str.c_str()[i] >= '0') && (str.c_str()[i] <= '9') ) )
            {
                //Not numeric
                return false;
            }
        }
        //numeric
        return true;
    }
    Code:
    #include <algorithm>
    #include <string>
    #include <cctype>
    
    bool IsNotDigit(char c)
    {
       return !isdigit(c);
    }
    
    bool check_string_numeric(const std::string& str)
    {
        return (std::find_if(str.begin(), str.end(), IsNotDigit) == str.end());
    }
    Regards,

    Paul McKenzie

  7. #7
    Join Date
    Jun 2006
    Location
    M31
    Posts
    885

    Re: std::string all numeric

    Quote Originally Posted by Paul McKenzie View Post
    Code:
    #include <algorithm>
    #include <string>
    #include <cctype>
    
    bool IsNotDigit(char c)
    {
       return !isdigit(c);
    }
    
    bool check_string_numeric(const std::string& str)
    {
        return (std::find_if(str.begin(), str.end(), IsNotDigit) == str.end());
    }
    Easier to just use isdigit directly:
    Code:
    bool check_string_numeric(const std::string& str)
    {
        return (std::find_if(str.begin(), str.end(), std::not1(std::ptr_fun(::isdigit))) == str.end());
    }
    If you've got access to the Boost SAL, this ugliness then becomes:
    Code:
    boost::all(str, boost::is_digit());

  8. #8
    Join Date
    Jan 2009
    Posts
    1,689

    Re: std::string all numeric

    I require an algorithm to do exactly this and the one I wrote hasn't failed me yet.

    This accepts any numeric value: decimal, hex, scientific notation, and has special recognition for Infinity and -Infinity.
    Code:
    char NumberChars[] = {"0123456789"};
    char HexChars[] = {"012345679ABCDEFabcdef"};
    
    string LS_INFINITY = "Infinity";
    string NEG_LS_INFINITY = "-Infinity";
    long double Infinity = 1.0f/0.0f;
    
    
    bool IsNumber(const string & str, long double & out){
        if (str == LS_INFINITY){
    	   out = Infinity;
    	   return true;
        } else if (str == NEG_LS_INFINITY){
    	   out = -Infinity;
    	   return true;
        }
        if (str[0] == '-'){
    	   if ((str[1] == '0') && (str[2] == 'x')){
    			if (str.find_first_not_of(HexChars, 3) != string::npos) return false;
    			long long tout;
    			stringstream ss;
    			ss.str(str);
    			ss << setbase(16);
    			ss >> tout;
    			out = (long double) tout;
    			return true;
    	   } else {  //negative, non hex, keep looking for failure
    			size_t pp = str.find_first_not_of(NumberChars, 1);
    			if (pp != string::npos){
    				if ((str[pp] == 'e') && ((str[pp + 1] == '+') || (str[pp + 1] == '-'))){  //negative scientifi notation
    					if (str.find_first_not_of(NumberChars, pp + 2) != string::npos) return false;
    				} else if (str[pp] == '.'){
    					pp = str.find_first_not_of(NumberChars, pp + 1);
    					if ((str[pp] == 'e') && ((str[pp + 1] == '+') || (str[pp + 1] == '-'))){  //negative scientifi notation
    						if (str.find_first_not_of(NumberChars) != string::npos) return false;
    					}
    				} else {
    					return false;
    				}
    			}
    	   }
        } else if ((str[0] == '0') && (str[1] == 'x')){
            if (str.find_first_not_of(HexChars, 2) != string::npos) return false;
        	long long tout;
    		stringstream ss;
    		ss.str(str);
    		ss << setbase(16);
    		ss >> tout;
    		out = (long double) tout;
    		return true;
        } else {
        	size_t pp = str.find_first_not_of(NumberChars);
    	    if (pp != string::npos){
    			if ((str[pp] == 'e') && ((str[pp + 1] == '+') || (str[pp + 1] == '-'))){  //negative scientifi notation
    				if (str.find_first_not_of(NumberChars, pp + 2) != string::npos) return false;
    			} else if (str[pp] == '.'){
    				pp = str.find_first_not_of(NumberChars, pp + 1);
    				if ((str[pp] == 'e') && ((str[pp + 1] == '+') || (str[pp + 1] == '-'))){  //negative scientifi notation
    					if (str.find_first_not_of(NumberChars) != string::npos) return false;
    				}
    			} else {
    				return false;
    			}
    	   	}
        }
        out = atof(str.c_str());
        return true;
    }
    Last edited by ninja9578; October 6th, 2009 at 04:36 PM.

  9. #9
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: std::string all numeric

    Quote Originally Posted by ninja9578
    I require an algorithm to do exactly this and the one I wrote hasn't failed me yet.
    Here is a call of your function that returns an incorrect result:
    Code:
    long double x;
    bool result = IsNumber("0xg", x);
    If you had written a helper function for your otherwise repeated hexadecimal conversion code you might have better avoided this mistake.

    Quote Originally Posted by ninja9578
    This accepts any numeric value: decimal, hex, scientific notation, and has special recognition for Infinity and -Infinity.
    But not octal, apparently

    Out of curiosity, but why do you write:
    Code:
    long double Infinity = 1.0f/0.0f;
    instead of using std::numeric_limits<long double>::infinity() or some equivalent existing constant?

    Oh, and of course you should declare those constants as const.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  10. #10
    Join Date
    Jan 2009
    Posts
    1,689

    Re: std::string all numeric

    Oops, fixed now 0xg will now fail as it should. I have no need for Octal because my scripting language doesn't support it

  11. #11
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725

    Re: std::string all numeric

    Some other strings to consider:

    "0X1"
    "1L"
    "1u"
    "0x"
    "+1"
    Last edited by Philip Nicoletti; October 6th, 2009 at 05:48 PM.

  12. #12
    Join Date
    May 2003
    Location
    San Antonio TX
    Posts
    380

    Re: std::string all numeric

    Thanks everyone
    John 3:16
    For God so loved the world ...

  13. #13
    Join Date
    Apr 1999
    Posts
    27,449

    Re: std::string all numeric

    Quote Originally Posted by Plasmator View Post
    Easier to just use isdigit directly:
    Yes, I know. My goal was to really take away the hand-written loop, and using the functor was easy in terms of not having to do the std::not1 stuff.

    Regards,

    Paul McKenzie

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured