Home >Backend Development >C++ >C++ program to check if a string is a number
Using strings or characters is very helpful when solving logic programming difficulties. Characters in a string are 1-byte data types that can store symbols in ASCII values. A string is a collection of characters. These symbols can be special characters, numbers from the number 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.
To check if a given string is a number, we need to check if each character in it is a number. If any of the characters is a non-numeric character, the string is non-numeric, otherwise it is numeric. The algorithm is as follows -
#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; }
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 if the given string is a number, but will not return true when the input is a negative number. For negative numbers, special checks are required.
To check if the given string is a number, we just need to check if each character is a number. But for negative numbers, the first character must be a "-" sign. So first check if the first character is negative, then check if the next character is a number, and if so, check if the remaining characters are numbers. The algorithm is as follows -
#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; }
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
To check if the given string is a number, we need to check every character of it. A string is a number when all characters are numbers. In this article, we also use logic to check negative numbers. When the first character is a negative sign, then check if the next character is a number. If yes, then check the rest. This program can be extended to check floating point numbers. Now it only works for positive and negative integers.
The above is the detailed content of C++ program to check if a string is a number. For more information, please follow other related articles on the PHP Chinese website!