Check if a given string is a valid number (Integer or Floating Point) | SET 1(Basic approach) Last Updated : 09 Mar, 2023 Comments Improve Suggest changes Like Article Like Report Validate if a given string is numeric. Examples: Input : str = "11.5" Output : true Input : str = "abc" Output : false Input : str = "2e10" Output : true Input : 10e5.4 Output : false The following cases need to be handled in the code. Ignore the leading and trailing white spaces.Ignore the '+', '-' and'.' at the start.Ensure that the characters in the string belong to {+, -, ., e, [0-9]}Ensure that no '.' comes after 'e'.A dot character '.' should be followed by a digit.The character 'e' should be followed either by '+', '-', or a digit. Below is implementation of above steps. C++ // C++ program to check if input number // is a valid number #include <bits/stdc++.h> #include <iostream> using namespace std; int valid_number(string str) { int i = 0, j = str.length() - 1; // Handling whitespaces while (i < str.length() && str[i] == ' ') i++; while (j >= 0 && str[j] == ' ') j--; if (i > j) return 0; // if string is of length 1 and the only // character is not a digit if (i == j && !(str[i] >= '0' && str[i] <= '9')) return 0; // If the 1st char is not '+', '-', '.' or digit if (str[i] != '.' && str[i] != '+' && str[i] != '-' && !(str[i] >= '0' && str[i] <= '9')) return 0; // To check if a '.' or 'e' is found in given // string. We use this flag to make sure that // either of them appear only once. bool flagDotOrE = false; for (i; i <= j; i++) { // If any of the char does not belong to // {digit, +, -, ., e} if (str[i] != 'e' && str[i] != '.' && str[i] != '+' && str[i] != '-' && !(str[i] >= '0' && str[i] <= '9')) return 0; if (str[i] == '.') { // checks if the char 'e' has already // occurred before '.' If yes, return 0. if (flagDotOrE == true) return 0; // If '.' is the last character. if (i + 1 > str.length()) return 0; // if '.' is not followed by a digit. if (!(str[i + 1] >= '0' && str[i + 1] <= '9')) return 0; } else if (str[i] == 'e') { // set flagDotOrE = 1 when e is encountered. flagDotOrE = true; // if there is no digit before 'e'. if (!(str[i - 1] >= '0' && str[i - 1] <= '9')) return 0; // If 'e' is the last Character if (i + 1 > str.length()) return 0; // if e is not followed either by // '+', '-' or a digit if (str[i + 1] != '+' && str[i + 1] != '-' && (str[i + 1] >= '0' && str[i] <= '9')) return 0; } } /* If the string skips all above cases, then it is numeric*/ return 1; } // Driver code int main() { char str[] = "0.1e10"; if (valid_number(str)) cout << "true"; else cout << "false"; return 0; } // This code is contributed by rahulkumawat2107 Java // Java program to check if input number // is a valid number import java.io.*; import java.util.*; class GFG { public boolean isValidNumeric(String str) { str = str.trim(); // trims the white spaces. if (str.length() == 0) return false; // if string is of length 1 and the only // character is not a digit if (str.length() == 1 && !Character.isDigit(str.charAt(0))) return false; // If the 1st char is not '+', '-', '.' or digit if (str.charAt(0) != '+' && str.charAt(0) != '-' && !Character.isDigit(str.charAt(0)) && str.charAt(0) != '.') return false; // To check if a '.' or 'e' is found in given // string. We use this flag to make sure that // either of them appear only once. boolean flagDotOrE = false; for (int i = 1; i < str.length(); i++) { // If any of the char does not belong to // {digit, +, -, ., e} if (!Character.isDigit(str.charAt(i)) && str.charAt(i) != 'e' && str.charAt(i) != '.' && str.charAt(i) != '+' && str.charAt(i) != '-') return false; if (str.charAt(i) == '.') { // checks if the char 'e' has already // occurred before '.' If yes, return 0. if (flagDotOrE == true) return false; // If '.' is the last character. if (i + 1 >= str.length()) return false; // if '.' is not followed by a digit. if (!Character.isDigit(str.charAt(i + 1))) return false; } else if (str.charAt(i) == 'e') { // set flagDotOrE = 1 when e is encountered. flagDotOrE = true; // if there is no digit before 'e'. if (!Character.isDigit(str.charAt(i - 1))) return false; // If 'e' is the last Character if (i + 1 >= str.length()) return false; // if e is not followed either by // '+', '-' or a digit if (!Character.isDigit(str.charAt(i + 1)) && str.charAt(i + 1) != '+' && str.charAt(i + 1) != '-') return false; } } /* If the string skips all above cases, then it is numeric*/ return true; } /* Driver Function to test isValidNumeric function */ public static void main(String[] args) { String input = "0.1e10"; GFG gfg = new GFG(); System.out.println(gfg.isValidNumeric(input)); } } Python3 # Python3 program to check if input number # is a valid number def valid_number(str): i = 0 j = len(str) - 1 # Handling whitespaces while i<len(str) and str[i] == ' ': i += 1 while j >= 0 and str[j] == ' ': j -= 1 if i > j: return False # if string is of length 1 and the only # character is not a digit if (i == j and not(str[i] >= '0' and str[i] <= '9')): return False # If the 1st char is not '+', '-', '.' or digit if (str[i] != '.' and str[i] != '+' and str[i] != '-' and not(str[i] >= '0' and str[i] <= '9')): return False # To check if a '.' or 'e' is found in given # string.We use this flag to make sure that # either of them appear only once. flagDotOrE = False for i in range(j + 1): # If any of the char does not belong to # {digit, +, -,., e} if (str[i] != 'e' and str[i] != '.' and str[i] != '+' and str[i] != '-' and not (str[i] >= '0' and str[i] <= '9')): return False if str[i] == '.': # check if the char e has already # occurred before '.' If yes, return 0 if flagDotOrE: return False if i + 1 > len(str): return False if (not(str[i + 1] >= '0' and str[i + 1] <= '9')): return False elif str[i] == 'e': # set flagDotOrE = 1 when e is encountered. flagDotOrE = True # if there is no digit before e if (not(str[i - 1] >= '0' and str[i - 1] <= '9')): return False # if e is the last character if i + 1 > len(str): return False # if e is not followed by # '+', '-' or a digit if (str[i + 1] != '+' and str[i + 1] != '-' and (str[i + 1] >= '0' and str[i] <= '9')): return False # If the string skips all the # above cases, it must be a numeric string return True # Driver Code if __name__ == '__main__': str = "0.1e10" if valid_number(str): print('true') else: print('false') # This code is contributed by # chaudhary_19 (Mayank Chaudhary) C# // C# program to check if input number // is a valid number using System; class GFG { public Boolean isValidNumeric(String str) { str = str.Trim(); // trims the white spaces. if (str.Length == 0) return false; // if string is of length 1 and the only // character is not a digit if (str.Length == 1 && !char.IsDigit(str[0])) return false; // If the 1st char is not '+', '-', '.' or digit if (str[0] != '+' && str[0] != '-' && !char.IsDigit(str[0]) && str[0] != '.') return false; // To check if a '.' or 'e' is found in given // string. We use this flag to make sure that // either of them appear only once. Boolean flagDotOrE = false; for (int i = 1; i < str.Length; i++) { // If any of the char does not belong to // {digit, +, -, ., e} if (!char.IsDigit(str[i]) && str[i] != 'e' && str[i] != '.' && str[i] != '+' && str[i] != '-') return false; if (str[i] == '.') { // checks if the char 'e' has already // occurred before '.' If yes, return 0. if (flagDotOrE == true) return false; // If '.' is the last character. if (i + 1 >= str.Length) return false; // if '.' is not followed by a digit. if (!char.IsDigit(str[i + 1])) return false; } else if (str[i] == 'e') { // set flagDotOrE = 1 when e is encountered. flagDotOrE = true; // if there is no digit before 'e'. if (!char.IsDigit(str[i - 1])) return false; // If 'e' is the last Character if (i + 1 >= str.Length) return false; // if e is not followed either by // '+', '-' or a digit if (!char.IsDigit(str[i + 1]) && str[i + 1] != '+' && str[i + 1] != '-') return false; } } /* If the string skips all above cases, then it is numeric*/ return true; } // Driver Code public static void Main(String[] args) { String input = "0.1e10"; GFG gfg = new GFG(); Console.WriteLine(gfg.isValidNumeric(input)); } } // This code is contributed by Rajput-Ji JavaScript // Javascript code for the above approach function valid_number(str) { let i = 0; let j = str.length - 1; // Handling whitespaces while (i < str.length && str[i] == ' ') { i++; } while (j >= 0 && str[j] == ' ') { j--; } if (i > j) { return false; } // if string is of length 1 and the only character is not a digit if (i == j && !(str[i] >= '0' && str[i] <= '9')) { return false; } // If the 1st char is not '+', '-', '.' or digit if ( str[i] != '.' && str[i] != '+' && str[i] != '-' && !(str[i] >= '0' && str[i] <= '9') ) { return false; } // To check if a '.' or 'e' is found in given string. // We use this flag to make sure that either of them appear only once. let flagDotOrE = false; for (let k = 0; k <= j; k++) { // If any of the char does not belong to {digit, +, -,., e} if ( str[k] != 'e' && str[k] != '.' && str[k] != '+' && str[k] != '-' && !(str[k] >= '0' && str[k] <= '9') ) { return false; } if (str[k] == '.') { // check if the char e has already occurred before '.' // If yes, return false if (flagDotOrE) { return false; } if (k + 1 > str.length) { return false; } if (!(str[k + 1] >= '0' && str[k + 1] <= '9')) { return false; } } else if (str[k] == 'e') { // set flagDotOrE = true when 'e' is encountered. flagDotOrE = true; // if there is no digit before 'e' if (!(str[k - 1] >= '0' && str[k - 1] <= '9')) { return false; } // if 'e' is the last character if (k + 1 > str.length) { return false; } // if 'e' is not followed by '+', '-' or a digit if ( str[k + 1] != '+' && str[k + 1] != '-' && !(str[k + 1] >= '0' && str[k + 1] <= '9') ) { return false; } } } // If the string skips all the above cases, it must be a numeric string return true; } // Driver Code let str = "0.1e10"; if (valid_number(str)) { console.log("true"); } else { console.log("false"); } // This code is contributed by adityashatmfh Output: true Time Complexity: O(n) Comment More infoAdvertise with us Next Article Check if a given string is a valid number (Integer or Floating Point) | SET 1(Basic approach) S Saloni Baweja Improve Article Tags : DSA Strings Practice Tags : Strings Similar Reads Check if a given string is a valid number (Integer or Floating Point) in Java | SET 2 (Regular Expression approach) In Set 1, we have discussed general approach to check whether a string is a valid number or not. In this post, we will discuss regular expression approach to check for a number. Examples: Input : str = "11.5" Output : true Input : str = "abc" Output : false Input : str = "2e10" Output : true Input : 3 min read Check if a given string is a valid number (Integer or Floating Point) in Java In the article Check if a given string is a valid number, we have discussed general approach to check whether a string is a valid number or not. In Java we can use Wrapper classes parse() methods along with try-catch blocks to check for a number. For integer number Integer class provides a static me 4 min read Check if given number contains only â01â and â10â as substring in its binary representation Given a number N, the task is to check if the binary representation of the number N has only "01" and "10" as a substring or not. If found to be true, then print "Yes". Otherwise, print "No".Examples: Input: N = 5 Output: Yes Explanation: (5)10 is (101)2 which contains only "01" and "10" as substrin 6 min read Program to construct a DFA to check if a given integer is unsigned or not Given a string S that represents an integer, the task is to check if the given string S represents an unsigned integer or not by constructing the DFA. If the given string represents an unsigned integer, then print "Unsigned integer". Otherwise, print "Not an unsigned integer". Examples: Input: S = " 10 min read Check if a number has bits in alternate pattern | Set-2 O(1) Approach Given a positive integer n. The problem is to check whether this integer has an alternate pattern in its binary representation or not. Here alternate pattern means that the set and unset bits in n occur in alternate order. For example- 5 has an alternate pattern i.e. 101. Print âYesâ if it has an al 6 min read Check whether given floating point number is even or odd Given a floating-point number, check whether it is even or odd. We can check whether a integer is even or odd by dividing its last digit by 2. But in case of floating point number we can't check a given number is even or odd by just dividing its last digit by 2. For example, 100.70 is an odd number 6 min read Check if a binary string has a 0 between 1s or not | Set 1 (General approach) Given a string of 0 and 1, we need to check that the given string is valid or not. The given string is valid when there is no zero present in between 1's. For example, 1111, 0000111110, 1111000 are valid strings but 01010011, 01010, 101 are not. One approach to solving the problem is discussed here, 11 min read Program to check if input is an integer or a string Write a function to check whether a given input is an integer or a string. Definition of an integer : Every element should be a valid digit, i.e '0-9'. Definition of a string : Any one element should be an invalid digit, i.e any symbol other than '0-9'. Examples: Input : 127Output : IntegerExplanati 15+ min read Check if a binary string has a 0 between 1s or not | Set 2 (Regular Expression Approach) Given a string of 0 and 1, we need to check that the given string is valid or not. The given string is valid when there is no zero is present in between 1's. For example, 1111, 0000111110, 1111000 are valid strings but 01010011, 01010, 101 are not. Examples: Input : 100 Output : VALID Input : 111000 3 min read Check if a number is in given base or not Given a number as a string and a base, check if the given number is in the given base or not. Examples: Input : str = "1010", base = 2 Output : Yes Input : str = "1015", base = 2 Output : No Input : str = "AF87", base = 16 Output : Yes The idea is to one by one check if all digits are in the given b 10 min read Like