Open In App

C++ Program to Check String is Containing Only Digits

Improve
Improve
Like Article
Like
Save
Share
Report

Prerequisite: Strings in C++

The string is the collection of characters or text in a string variable, surrounded by double quotes. One question arises How can we check string contains only digits in C++? So, to solve this query we can use the methods mentioned in the article given below:

Example:

“125”     -> True

“1h25”   -> False

“012”     -> True

Methods to check string is containing only digits in C++

There are a couple of methods to check string contains numbers these are:

  1. Using ASCII value
  2. Using isdigit( )
  3. Using all_of( ) with isdigit( )

1. Checking using ASCII Value

Every character has its own unique ASCII value, So to check if the string is a number we can just check the ASCII values of the characters in the string and if all the characters are numbers we can say that the string contains digits only.

The ASCII value of '0' is 48 , '9' is 57.

Example:

C++




// C++ Program to demonstrate
// Using ASCII value
#include <iostream>
using namespace std;
 
void is_digits(string& str)
{
    for (char ch : str) {
        int v = ch; // ASCII Val converted
        if (!(ch >= 48 && ch <= 57)) {
            cout << "False" << endl;
            return;
        }
    }
 
    cout << "True" << endl;
}
 
// Driver Code
int main()
{
    string str = "125";
    is_digits(str);
 
    str = "1h34";
    is_digits(str);
 
    str = "012";
    is_digits(str);
 
    return 0;
}


Output

True
False
True

2. Checking using isdigit( )

is_digit is a function used to check whether the character is a number or not. So, we can use this function over a Loop and check one by one if the character is a number or not.

Example:

C++




#include <bits/stdc++.h>
using namespace std;
 
void is_digits(string& str)
{
    for (char ch : str) {
        if (!isdigit(ch)){
              cout << "False" << endl;
            return;
        }
    }
 
    cout << "True" << endl;
}
 
int main()
{
    string str = "125";
    is_digits(str);
 
    str = "1h34";
    is_digits(str);
 
    str = "012";
    is_digits(str);
 
    return 0;
}


Output

True
False
True

3. Checking using all_of() with isdigit() 

all_of and isdigit together can check if all the characters in a string are digits or not. These two are rebuild functions to perform this task.

Syntax:

all_of(string_name.begin(), str_name.end(), ::isdigit);

Explanation of the functions:

isdigit procedure is used.

  • int isdigit( int ch );
  • compares the character passed ch is either ‘0”1’…’9’

all_of() 

  • It runs the given method on the iteratable one by one element and behaves as AND operator. 
  • If all the elements returns True, all_of returns True. 
  • Even one false, leads to return False. 

Example:

C++




// C++ Program to check method
// using all_of() with isdigit()
#include <bits/stdc++.h>
using namespace std;
 
// Print boolean value
void print(bool x)
{
    if (x)
        cout << "True" << endl;
    else
        cout << "False" << endl;
}
 
// Function to check
bool is_digits(string& str)
{
    return all_of(str.begin(), str.end(), ::isdigit);
}
 
// Driver Code
int main()
{
    string str = "125";
    print(is_digits(str));
 
    str = "1h34";
    print(is_digits(str));
 
    str = "012";
    print(is_digits(str));
 
    return 0;
}


Output

True
False
True


Last Updated : 27 Jan, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads