
- 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
Check if a given string is a valid number in C++
Concept
It should be validated if a given string is numeric.
Input − str = "12.5"
Output − true
Input − str = "def"
Output − false
Input − str = "2e5"
Output − true
Input − 10e4.4
Output − false
Method
We have to handle the following cases in the code.
We have to ignore the leading and trailing white spaces.
We have to ignore the ‘+’, ‘-‘ and’.’ at the start.
We have to ensure that the characters in the string belong to {+, -, ., e, [0-9]}
We have to ensure that no ‘.’ comes after ‘e’.
A digit should follow a dot character ‘.’.
We have to ensure that the character ‘e’ should be followed either by ‘+’, ‘-‘, or a digit.
Example
// C++ program to check if input number // is a valid number #include <bits/stdc++.h> #include <iostream> using namespace std; int valid_number1(string str1){ int i = 0, j = str1.length() - 1; while (i < str1.length() && str1[i] == ' ') i++; while (j >= 0 && str1[j] == ' ') j--; if (i > j) return 0; if (i == j && !(str1[i] >= '0' && str1[i] <= '9')) return 0; if (str1[i] != '.' && str1[i] != '+' && str1[i] != '-' && !(str1[i] >= '0' && str1[i] <= '9')) return 0; bool flagDotOrE = false; for (i; i <= j; i++) { // If any of the char does not belong to // {digit, +, -, ., e} if (str1[i] != 'e' && str1[i] != '.' && str1[i] != '+' && str1[i] != '-' && !(str1[i] >= '0' && str1[i] <= '9')) return 0; if (str1[i] == '.') { if (flagDotOrE == true) return 0; if (i + 1 > str1.length()) return 0; if (!(str1[i + 1] >= '0' && str1[i + 1] <= '9')) return 0; } else if (str1[i] == 'e') { flagDotOrE = true; if (!(str1[i - 1] >= '0' && str1[i - 1] <= '9')) return 0; if (i + 1 > str1.length()) return 0; if (str1[i + 1] != '+' && str1[i + 1] != '-' && (str1[i + 1] >= '0' && str1[i] <= '9')) return 0; } } return 1; } // Driver code int main(){ char str1[] = "0.1e10"; if (valid_number1(str1)) cout << "true"; else cout << "false"; return 0; }
Output
true
- Related Articles
- Check if a given string is a valid number in Python
- Java Program to check if a string is a valid number
- How to check if a string is a valid keyword in C#?
- Check if a given string is sum-string in C++
- Check whether the given string is a valid identifier in Python
- How to check if a string is a valid keyword in Python?
- How to check if a string is a valid keyword in Java?
- How to check if a string is a valid URL in Golang?
- Check if a given number is Pronic in C++
- Python program to check if a given string is number Palindrome
- Check if a given mobile number is fancy in C++
- C Program to Check if a Given String is a Palindrome?
- Check if a given string is a rotation of a palindrome in C++
- Check If a String Is a Valid Sequence from Root to Leaves Path in a Binary Tree in C++
- C# program to check if a Substring is present in a Given String

Advertisements