How to check if string is a number in C++

Check if string is a number in C++

In this tutorial, we will see how to check if string is a number in C++.

Every application in C++ is designed for a unique purpose. And to fulfil these purposes we make use of the unique data types offered by the C++ programming language.

For example: Character, Boolean, Floating Point, Double Floating Point, Integer, String etc.

💻 Awesome Tech Resources:
  • Looking for ⚒️ tech jobs? Go to our job portal.
  • Looking for tech events? Go to tech events 🗓️ Calendar.️

Each one of these data types occupy a specific amount of memory space, determining the size and the layout of the variable’s memory map. To specify these data types in the program we make use of keywords. These variable data types help us to manipulate data however we please.

In this tutorial we will dive deep in two of these variable data types.

  1. String
  2. Integer

String

  • A string variable carries a collection of characters, these characters may include alphabets/ letters or other characters.
  • All the characters in string variable are always between double quotes.

Example: // Include the string library

Here, string variable is Welcome to Java2Blog

Integer

  • An Integer carries a whole number value, like 25 or 23645.
  • They have variable sizes.
  • Integers can be signed or unsigned, depending upon whether they support negative or positive value.

Example:

Here, the integer is 765

Sometimes some applications need to identify whether a string is a number or not. There several ways to check if String is number in C++.

Below are the programs that can help users to identify if a string is a number.

Using std::isdigit() method to check if a string is number.

This is the most common method used for the solution of this problem.
In this method, we pass a string as a parameter to the isNumber() function. It iterates over every character present in the string and check with isdigit method.

When it detects the first non-digit, the function returns false and displays message accordingly.

If not a single non-digit is found, the function returns true and the appropriate message is displayed.

Output:

Java2Blog is Not a number
C++ is Not a number
5189746 is a Number

Using find_first_not_of() method to check if a String Is a Number.

This program has an inbuilt algorithm to search for strings.
It searches all the strings for characters, if the character is not found, i.e. if a non-digit is not identified it returns string :: npos.
The result of all the checked strings is then displayed with the help of isNumber.

Output:

Java2Blog is Not a number
6767526 is a Number
#$&&* is Not a number

Using std::isdigit and std::all_of methods to check if String is a Number (C++11 onwards)

You can also use all_of() with isdigit() method to check if String is a Number or not.

all_of() can save time to run a loop to check each items one by one. It checks for given property on each element one by one and returns true if all the elements satisfy the property.

Output:

Java is Not a number
4572232 is a Number
Python123 is Not a number

Using std::isdigit and std::find_if methods to check if String is a Number (C++11 onwards)

You can also use find_if() with isdigit() method to check if String is a Number or not.

all_of() can save time to run a loop to check each items one by one. It checks for given property on each element one by one and returns true if all the elements satisfy the property.

Output:

abc123 is Not a number
654343 is a Number
m1232 is Not a number

The above foue methods show how to identify if a string is a number when the string to be identified was in the code itself. But many applications need to determine whether a string entered by the user is a number or not.

How to check if user input is number or not.

In this program, we will take an input from the user in a string named checkint.
After initialization we calculate the length of the string and try to identify the first non-digit.
Depending upon the input the final message is displayed.

Output:

Enter a string : 32143
Is an Integer

These are the various methods by which a string can be easily identified as a number.

Uses

The operation of identifying a string as a number or not is used in many places. The most common example is Roll number system in schools/ collages. Where a student’s roll number needs to be entered in the system. If the correct roll number (digits only) is entered, the system proceeds, otherwise it shows an error message.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.