
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
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
- if c is non-digit, then
- 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
- if c is non-digit, then
- 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.
- Related Articles
- Swift Program to Check if a String is Numeric
- Java Program to Check if a String is Numeric
- Golang Program to Check if a String is Numeric
- Haskell Program to Check if a String is Numeric
- How to check if input is numeric in C++?
- C Program to Check if a Given String is a Palindrome?
- C# program to check if a string is palindrome or not
- C# program to check if string is panagram or not
- C program to check if a given string is Keyword or not?
- C++ Program to check if input is an integer or a string
- C# program to check if a Substring is present in a Given String
- How to check if a unicode string contains only numeric characters in Python?
- Python Program to check whether all elements in a string list are numeric
- Java program to check if string is pangram
- C# program to check if a string contains any special character
