Check if string is 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.

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

  • Trim String in C++
    19 July

    Trim String in C++

    Table of ContentsUsing Boost string algorithms to trim string in C++Using find_first_not_of() and find_last_not_of()` to trim strings in C++Using find_if() to trim the string in C++Using stringstream to trim the strings in C++Using a customized function to trim the strings in C++Conclusion When we take an input from an user, strings can have unwanted whitespaces […]

  • JSON parser in C++
    19 July

    JSON Parser in C++

    Table of ContentsSimple JSON Parser in C++ using JsonCpp libraryConclusion In this post, we will see about JSON parser in C++. There is no native support for JSON in C++. We can use a number of libraries that provide support for JSON in C++. We will use JsonCpp to parse JSON files in C++ which […]

  • Enum to String in C++
    19 July

    Convert enum to string in C++

    Table of ContentsUsing stringify macro method to convert enum to String in C++Using const char* Array to convert enum to String in C++Using a custom-defined function to convert enum to String in C++Conclusion There are a variety of methods to convert enum to String in C++. In this article, we will discuss some of the […]

  • C++ random number between 0 and 1
    11 June

    C++ random number between 0 and 1

    Table of ContentsUsing the rand() function to generate random number between 0 and 1 in C++Using the std::uniform_real_distribution() function to generate random number between 0 and 1 in C++ Random numbers are frequently used in programming for test case generation, cryptography, and other related purposes. In this tutorial, we will generate random number between 0 […]

  • String contains C++
    07 June

    Check if string contains substring in C++

    Table of ContentsUsing find() function to check if string contains substring in C++.Using strstr() method to check if string contains substring in C++Using boost library to check if string contains substring in C++Using custom functions to check if string contains substring in C++Conclusion There are different ways to check if string contains substring in C++. […]

  • Convert float to String in C++
    24 May

    Convert float to String in C++

    Learn about how to convert float to String in C++ in different wayds.

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.