C++ Program to Check if a String is Numeric


It can be quite helpful to use strings or characters while tackling logical programming difficulties. Characters in strings are 1-byte data types that can store symbols from ASCII values. Strings are collections of characters. The symbols could be special characters, numerals from the numeric system, or letters from the English alphabet. This article will teach you how to use C++ to determine whether a character is a numeric character or not.

Checking string is numeric or not

To check whether the given string is numeric or not, we need to check each character in it is a digit or not. If any one of them is a non-digit character then the string is non-numeric, otherwise, it is numeric. The algorithm will be like the below −

Algorithm

  • read a string s as input
  • for each character c in s, do
    • if c is non-digit, then
      • return false
    • end if
  • end for
  • return true

Example

#include <iostream> #include <ctype.h> using namespace std; string solve( string s ) { for( int i = 0; i < s.length(); i++ ) { if( !isdigit( s[i] )) { return "False"; } } return "True"; } int main() { cout << "Is "589" a numeric string? : " << solve( "589" ) << endl; cout << "Is "69a" a numeric string? : " << solve( "69a" ) << endl; cout << "Is "2979624" a numeric string? : " << solve( "2979624" ) << endl; cout << "Is "25\%4A" a numeric string? : " << solve( "25\%4A" ) << endl; cout << "Is "889" a numeric string? : " << solve( "889" ) << endl; }

Output

Is "589" a numeric string? : True
Is "69a" a numeric string? : False
Is "2979624" a numeric string? : True
Is "25%4A" a numeric string? : False
Is "889" a numeric string? : True

This solution can check given string is numeric or not, but it does not return true when the input is negative. For the negative numbers, special checking is required.

Checking string is numeric with negative or positive

Checking whether a given string is numeric or not, we just check whether each character is a digit or not. But for negative numbers, the first character must be a ‘-‘ sign. So check if the first character is a negative number, then the next character is a digit, if so, check rest is a digit or not. The algorithm will be like the below −

Algorithm

  • read a string s as input
  • if first character of s is '-' and next character is a digit, then
    • st = 1
  • otherwise
    • st = 0
  • end if
  • for each character c in s starting from index st, do
    • if c is non-digit, then
      • return false
    • end if
  • end for
  • return true

Example

#include <iostream> #include <ctype.h> using namespace std; string solve( string s ) { int start; if( s[0] == '-' && isdigit( s[1] ) ) { start = 1; } else { start = 0; } for( int i = start; i < s.length(); i++ ) { if( !isdigit( s[i] )) { return "False"; } } return "True"; } int main() { cout << "Is "687" a numeric string? : " << solve( "687" ) << endl; cout << "Is "256l" a numeric string? : " << solve( "256l" ) << endl; cout << "Is "-5845" a numeric string? : " << solve( "-5845" ) << endl; cout << "Is "-89A2" a numeric string? : " << solve( "-89A2" ) << endl; cout << "Is "-256" a numeric string? : " << solve( "-256" ) << endl; }

Output

Is "687" a numeric string? : True
Is "256l" a numeric string? : False
Is "-5845" a numeric string? : True
Is "-89A2" a numeric string? : False
Is "-256" a numeric string? : True

Conclusion

Checking whether a given string is numeric or not, we need to check each character of it. When all characters are numeric, then the string is numeric. In this article, we have also used logic to check negative numbers. When the first character is a negative sign, then check next character is numeric or not. If yes then check the rest. This program can be extended to check floating point numbers. Now it will only work on positive and negative integers.

Updated on: 19-Oct-2022

9K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements