how to check if given c++ string or char* contains only digits?
Of course, there are many ways to test a string for only numeric characters. Two possible methods are:
bool is_digits(const std::string &str)
{
return str.find_first_not_of("0123456789") == std::string::npos;
}
or
bool is_digits(const std::string &str)
{
return std::all_of(str.begin(), str.end(), ::isdigit); // C++11
}
how to check if the string contains only numbers or word in c
*str
this is a pointer to the first character of the string so along your cycle it only checks this first character.
Furthermore you are returning values before checking the entire string, you need to use flags and set them along the cycle for digits caps and small letters, and in the end return the values according to those flags.
There is the <ctype.h>
library which has functions like isalpha
and isdigit
and can make your job easier, for that I refer you to @DevSolar answer as a better method.
If you can't use it this is the way to go:
Live demo
int categorize(char *str) {
int x = 0;
int is_digit, is_cap, is_small;
is_cap = is_digit = is_small = 0;
while (str[x] != '\0') {
if (!(str[x] >= 'a' && str[x] <= 'z') && !(str[x] >= 'A' && str[x] <= 'Z') && !(str[x] >= '0' && str[x] <= '9'))
return 6;
if (str[x] >= 'A' && str[x] <= 'Z' && !is_cap) //if there are caps
is_cap = 1;
if (str[x] >= '0' && str[x] <= '9' && !is_digit) //if there are digits
is_digit = 1;
if (str[x] >= 'a' && str[x] <= 'z' && !is_small) //if there are smalls
is_small = 1;
x++;
}
if ((is_small || is_cap) && is_digit){
return 5;
}
if(is_small && is_cap){
return 3;
}
if(is_digit){
return 4;
}
if(is_small){
return 2;
}
if(is_cap){
return 1;
}
return 6;
}
Note that this kind of character arithmetic works well in ASCII but fails in other character encodings like EBCDIC which don't have sequencial order for alphabetic characters.
Fastest way to check if string contains only digits in C#
bool IsDigitsOnly(string str)
{
foreach (char c in str)
{
if (c < '0' || c > '9')
return false;
}
return true;
}
Will probably be the fastest way to do it.
How to check if a string is a number?
Forget about ASCII code checks, use isdigit
or isnumber
(see man isnumber
). The first function checks whether the character is 0–9, the second one also accepts various other number characters depending on the current locale.
There may even be better functions to do the check – the important lesson is that this is a bit more complex than it looks, because the precise definition of a “number string” depends on the particular locale and the string encoding.
Related Topics
Foreach Macro on Macros Arguments
C++ Custom Stream Manipulator That Changes Next Item on Stream
Difference Between a Pointer and Reference Parameter
Behavior of Post Increment in Cout
Order of Member Constructor and Destructor Calls
Is the 'Override' Keyword Just a Check For a Overridden Virtual Method
How to Code a Modulo (%) Operator in C/C++/Obj-C That Handles Negative Numbers
Ternary Operator : VS If...Else
Should I Use an Exception Specifier in C++