C++ Program to Check String is Containing Only Digits Last Updated : 27 Jan, 2023 Comments Improve Suggest changes Like Article Like 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: Using ASCII valueUsing isdigit( )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; } OutputTrue 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; } OutputTrue 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; } OutputTrue False True Comment More infoAdvertise with us Next Article C++ Program to Check String is Containing Only Digits rbkraj000 Follow Improve Article Tags : Technical Scripter C++ Programs C++ Technical Scripter 2022 cpp-strings +1 More Practice Tags : CPPcpp-strings Similar Reads C++ Program For char to int Conversion In C++, we cannot directly perform numeric operations on characters that represent numeric values. If we attempt to do so, the program will interpret the character's ASCII value instead of the numeric value it represents. We need to convert the character into an integer.Example:Input: '9'Output: 9Ex 2 min read C++ Program to Check Whether a Number is Palindrome or Not A palindrome number is a number that remains the same even if its digits are reversed. In this article, we will learn how to check a given number is a palindrome or not in C++.The easiest way to check if a number is a palindrome is to simply reverse the original number, then check if both numbers ar 4 min read How to Output Error When Input Isn't a Number in C++? In C++, when taking user input, we may need to validate that it is a number and output an error if it is not. In this article, we will learn how to output an error when the input isn't a number in C++. For Example, Input:Enter a Number: GOutput:Error: That was not a number.Output Error When Input Is 2 min read Extract all integers from a given String Given a string str, extract all integers words from it. Example: Input: str = "1Hello2 &* how are y5ou"Output: 1 2 5 Input: str = "Hey everyone, I have 500 rupees and I would spend a 100"Output: 500 100 Approach: To solve this problem follow the below steps: Create a string tillNow, which will s 9 min read C++ Program to Convert String to Integer Given a string of digits, the task is to convert the string to an integer. Examples: Input : str = "12345" Output : 12345 Input : str = "876538"; Output : 876538 Input : str = "0028"; Output : 28 CPP // C++ program to convert String into Integer #include <bits/stdc++.h> using namespace std; // 1 min read Move all digits to the beginning of a given string Given a string S, the task is to move all the digits present in the string, to the beginning of the string. Examples: Input: S = âGeeks4forGeeks123âOutput: 4123GeeksforGeeksExplanation:The given string contains digits 4, 1, 2, and 3. Moving all the digits to the beginning of the string modifies the 7 min read Check if the Sum of Digits is Palindrome or not Given an integer n, the task is to check whether the sum of digits of n is palindrome or not.Example: Input: n = 56 Output: trueExplanation: Digit sum is (5 + 6) = 11, which is a palindrome.Input: n = 51241 Output: falseExplanation: Digit sum is (5 + 1 + 2 + 4 + 1) = 13, which is not a palindrome.Ta 9 min read Program to check for a Valid IMEI Number International Mobile Equipment Identity (IMEI) is a number, usually unique, to identify mobile phones, as well as some satellite phones. It is usually found printed inside the battery compartment of the phone, but can also be displayed on-screen on most phones by entering *#06# on the dialpad, or al 6 min read Queries to check whether a given digit is present in the given Range Pre-requisites: Segment Tree Given an array of digits arr[]. Given a number of range [L, R] and a digit X with each range. The task is to check for each given range [L, R] whether the digit X is present within that range in the array arr[]. Examples: Input : arr = [1, 3, 3, 9, 8, 7] l1=0, r1=3, x=2 11 min read Convert given string to a valid mobile number Given a string M consisting of letters, digits and symbols, the task is to convert the string to a valid mobile number by removing all characters except the digits in the following format: Form a substring of 3 digits while the length of the remaining string is greater than 3.Enclose each substring 7 min read Like