# Check if a string is a number in C++

In this article, we will discuss different ways to check if a string is a number or not in C++. Basically, we will confirm that string contains only digits.

## Problem description

In this article, we have given a string and we have to check whether the string is a number or not with the help of C++ STL. Our C++ program can iterate the string and check if all the characters of string is a number or not by different methods.

Input

```string s1 = "54321";
string s2 = "-12345";
string s3 = "a1b2c3";
```

Output

```s1 is a Number
s2 is a Number
s3 is not a Number
```

## Method 1: Using Loop

In this method we are using a loop to check whether every character is a number or not. There are three string declared in the program and each string is passed in isNumeric() function which is a boolean function and return either true or false. Inside the isNumeric() function we are iterating the string with the help of iterator. Increment of iterator is done only if current character of string is a number, which can be checked by isdigit() function. The isNumeric function returns true only if the iterator reached to end of the string. This method fails to give correct answer in case of negative number present in string.

`Time Complexity: O(n)`
`Space Complexity: O(1)`

```#include <iostream>
#include <string>

using namespace std;

bool isNumber(string str)
{
// Declare the string iterator
string::iterator it = str.begin();

// Iterating in string with loop
while (it != str.end() && isdigit(*it))
{
// Increment the iterator
it++;
}
// Return true if iterator goes to end of string
return !str.empty() && it == str.end();
}

int main()
{
// Define three strings
string s1 = "54321";
string s2 = "-12345";
string s3 = "a1b2c3";

// check the string is a number by calling the isNumeric() function
cout << "s1 is " << (isNumber(s1) ? "a Number" : "not a Number") << endl;
cout << "s2 is " << (isNumber(s2) ? "a Number" : "not a Number") << endl;
cout << "s3 is " << (isNumber(s3) ? "a Number" : "not a Number") << endl;

return 0;
}
```

Output

```s1 is a Number
s2 is not a Number
s3 is not a Number
```

## Method 2: Using STL Algorithm std::find_if()

In this method we are using `find_if()` and check whether every character is a number or not. There are three string declared in the program and each string is passed in `isNumeric()` function which is a boolean function and return either true or false. Inside the `isNumeric()` function we are iterating the string with `find_if()` function. The `find_if()` function have three arguments: `begin` iterator, `end` iterator and `boolean function`. In the boolean function the characters of string are checked whether they are numbers or not with the help of `isdigit()` function. The `find_if()` function return the iterator which points the character of string where the boolean function (of find_if()) returns flase. The `isNumeric()` function returns true only if the iterator reached to end of the string. This method fails to give correct answer in case of negative number present in string.

`Time Complexity: O(n)`
`Space Complexity: O(1)`

```#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

bool isNumber(string str)
{
// Use of find_if() function
auto it = find_if(str.begin(), str.end(), [](char const &c) {
return !isdigit(c);
});

// Return true if iterator goes to end
return !str.empty() && it == str.end();
}

int main()
{
// Define three strings
string s1 = "54321";
string s2 = "-12345";
string s3 = "a1b2c3";

// check the string is a number by calling the isNumeric() function
cout << "s1 is " << (isNumber(s1) ? "a Number" : "not a Number") << endl;
cout << "s2 is " << (isNumber(s2) ? "a Number" : "not a Number") << endl;
cout << "s3 is " << (isNumber(s3) ? "a Number" : "not a Number") << endl;

return 0;
}
```

Output

```s1 is a Number
s2 is not a Number
s3 is not a Number
```

## Method 3: Using STL Algorithm std::all_of()

In this method we are using `all_of()` function and check whether every character is a number or not. There are three string declared in the program and each string is passed in `isNumeric()` function which is a boolean function and return either true or false. Inside the `isNumeric()` function we are iterating the string with the help of `all_of()` function which have 3 arguments: `begin` iterator, `end` iterator and `boolean function`. The boolean function used here is `isdigit()` function which checks whether a character is numeric or not. This way `all_of()` function returns true only if all the characters in string is numeric. The `isNumeric()` function returns true only if the `all_of()` function returns true. This method fails to give correct answer in case of negative number present in string.

`Time Complexity: O(n)`
`Space Complexity: O(1)`

```#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

bool isNumber(string str)
{
// Return true if all elemets of string is a number
return !str.empty() && all_of(str.begin(), str.end(), ::isdigit);
}

int main()
{
// Define three strings
string s1 = "54321";
string s2 = "-12345";
string s3 = "a1b2c3";

// check the string is a number by calling the isNumeric() function
cout << "s1 is " << (isNumber(s1) ? "a Number" : "not a Number") << endl;
cout << "s2 is " << (isNumber(s2) ? "a Number" : "not a Number") << endl;
cout << "s3 is " << (isNumber(s3) ? "a Number" : "not a Number") << endl;

return 0;
}
```

Output

```s1 is a Number
s2 is not a Number
s3 is not a Number
```

## Method 4: Using strtol() function

In this method we are using `strtol()` function and check whether every character is a number or not. There are three string declared in the program and each string is passed in `isNumeric()` function which is a boolean function and return either true or false. Inside the `isNumeric()` function we are using `strtol()` function which is used to convert the string into a number with a specified base. The strtol function takes 3 arguments as a input: C-string, char pointer and base of number (for decimal number base is 10). The conversion of string to C-string is done by c_str() function.
The isNumeric() function returns true only if character pointer is null. This approach is good for positive as well as negative numbers present in the string.

`Time Complexity: O(n)`
`Space Complexity: O(1)`

```#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

bool isNumber(string str)
{
// Declare the char pointer
char* p;

// Use of strtol() function with C-string, char pointer and base as a input
strtol(str.c_str(), &p, 10);

// Return true if char pointer p is null
return *p == 0;
}

int main()
{
// Define three strings
string s1 = "54321";
string s2 = "-12345";
string s3 = "a1b2c3";

// check the string is a number by calling the isNumeric() function
cout << "s1 is " << (isNumber(s1) ? "a Number" : "not a Number") << endl;
cout << "s2 is " << (isNumber(s2) ? "a Number" : "not a Number") << endl;
cout << "s3 is " <<  (isNumber(s3) ? "a Number" : "not a Number") << endl;

return 0;
}
```

Output

```s1 is a Number
s2 is a Number
s3 is not a Number
```

## Summary

Today, we learned about various methods to check if a string is a number or not. Each method has its own time and space complexity. Thanks.