It can often be necessary to see whether a string contains a number. In most cases, we must check whether a string is numeric or contains a number. We can use isdigit()
, a built-in function from the std
namespace, for both tasks, as it returns true
if the input passed to it is a digit.
Suppose we check whether a string contains numbers without additional characters such as text, space, special characters, etc. In that case, we will check each of its characters with the built-in function isdigit()
using iteration. If the function returns true
for all characters during the iteration, only then will the string be numeric. If the function returns false
for any character within the string, the loop will stop iterating, proving the string is not numeric.
#include <iostream>#include <string>using namespace std;//Following function will get the string as an argument and find out whether its numeric or not.void numericstring(string str){cout << "Is string '" << str << "' is a numeric string?" << endl;// Following loop will check each character of string. If any character is not a digit, it will change flag to false.bool flag = true; //We initialise flag as true.for (int i=0; i<str.length(); i++){if (isdigit(str[i]) == false){flag = false;break;}}if (flag == true){ // It means that the previous for loop found each character to be a digit.cout << "True" << endl;}else{ // It means that the previous for loop found a non-digit character.cout << "False" << endl;}}int main() {string str;str = "1234"; // A numeric stringnumericstring(str); // String passed to the functioncout << "\n" << string(100, '-') << "\n\n"; // Line breakstring str1;str1 = "12 34"; // A non-numeric stringnumericstring(str1); // String passed to the functionreturn 0;}
Line 7: A void
function numericstring
is declared. A string will be passed to it, and the function will display to the user whether the string is numeric.
Line 11: A boolean flag
is initialised as true
. This flag
is going to be used as the result of our check on the string being numeric.
Lines 12–17: A for
loop starts, within which each character of the first given string is checked with the isdigit()
function. If a non-numeric character is found, the flag is turned false
and the loop is stopped.
Lines 30 and 36: The numericstring
function is called in both these lines with a different string passed to the function each time.
The code above, however, will only work when the string contains 0 or positive integers.
If we’re checking whether a string contains numbers, alongside other characters, then the for
loop used in the previous code, will be slightly altered.
Each character will be checked, like before, with the isdigit()
function in an iteration. However, if the function returns false
for all the characters during the iteration, only then will the string not be containing any numeric value at all. If the function returns true
for any character within the string, the loop will stop iterating; it will prove the string contains a number.
#include <iostream>#include <string>using namespace std;//Following function will get the string as an argument and find out whether it contains any number.void stringhasnumber (string str){cout << "Does the string '" << str << "' contain a number?" << endl;// Following loop will check each character of string. If any character is a digit, it will change flag to true.bool flag = false; // Initialising flag as false.for (int i=0; i<str.length(); i++){if (isdigit(str[i]) == true){flag = true;break;}}if (flag == true){ // It means a digit was found within the string during the previous for loop.cout << "True" << endl;}else{ // It means the no number was found during the previous for loop.cout << "False" << endl;}}int main() {string str;str = "There are 2 humans here."; // A string containing a numberstringhasnumber(str); // String passed to the functioncout << "\n" << string(100, '-') << "\n\n"; // Line breakstring str1;str1 = "There are some humans here."; // A string without a numberstringhasnumber(str1); // String passed to the functionreturn 0;}
Line 7: A void
function stringhasnumber
is declared. A string will be passed to it, and the function will display to the user whether the string contains any number.
Line 11: A boolean flag
is initialised as false
. This flag
is going to be used as the result of our check on the first string containing any number.
Lines 12–17: A for
loop starts, within which each character of the first given string is checked with the isdigit()
function. If a numeric character is found, the flag is turned true
and the loop is stopped.
Lines 30 and 36: The stringhasnumber
function is called in both these lines, with a different string passed to the function each time.
The code above will work for all possible cases, including cases where the string contains 0
, natural numbers, decimals, negative numbers, etc.
Free Resources