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++
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
int valid_number(string str)
{
int i = 0, j = str.length() - 1;
while (i < str.length() && str[i] == ' ' )
i++;
while (j >= 0 && str[j] == ' ' )
j--;
if (i > j)
return 0;
if (i == j && !(str[i] >= '0' && str[i] <= '9' ))
return 0;
if (str[i] != '.' && str[i] != '+'
&& str[i] != '-' && !(str[i] >= '0' && str[i] <= '9' ))
return 0;
bool flagDotOrE = false ;
for (i; i <= j; i++) {
if (str[i] != 'e' && str[i] != '.'
&& str[i] != '+' && str[i] != '-'
&& !(str[i] >= '0' && str[i] <= '9' ))
return 0;
if (str[i] == '.' ) {
if (flagDotOrE == true )
return 0;
if (i + 1 > str.length())
return 0;
if (!(str[i + 1] >= '0' && str[i + 1] <= '9' ))
return 0;
}
else if (str[i] == 'e' ) {
flagDotOrE = true ;
if (!(str[i - 1] >= '0' && str[i - 1] <= '9' ))
return 0;
if (i + 1 > str.length())
return 0;
if (str[i + 1] != '+' && str[i + 1] != '-'
&& (str[i + 1] >= '0' && str[i] <= '9' ))
return 0;
}
}
return 1;
}
int main()
{
char str[] = "0.1e10" ;
if (valid_number(str))
cout << "true" ;
else
cout << "false" ;
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
public boolean isValidNumeric(String str)
{
str = str.trim();
if (str.length() == 0 )
return false ;
if (str.length() == 1 && !Character.isDigit(str.charAt( 0 )))
return false ;
if (str.charAt( 0 ) != '+' && str.charAt( 0 ) != '-'
&& !Character.isDigit(str.charAt( 0 ))
&& str.charAt( 0 ) != '.' )
return false ;
boolean flagDotOrE = false ;
for ( int i = 1 ; i < str.length(); i++) {
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) == '.' ) {
if (flagDotOrE == true )
return false ;
if (i + 1 >= str.length())
return false ;
if (!Character.isDigit(str.charAt(i + 1 )))
return false ;
}
else if (str.charAt(i) == 'e' ) {
flagDotOrE = true ;
if (!Character.isDigit(str.charAt(i - 1 )))
return false ;
if (i + 1 >= str.length())
return false ;
if (!Character.isDigit(str.charAt(i + 1 ))
&& str.charAt(i + 1 ) != '+'
&& str.charAt(i + 1 ) != '-' )
return false ;
}
}
return true ;
}
public static void main(String[] args)
{
String input = "0.1e10" ;
GFG gfg = new GFG();
System.out.println(gfg.isValidNumeric(input));
}
}
|
Python3
def valid_number( str ):
i = 0
j = len ( str ) - 1
while i< len ( str ) and str [i] = = ' ' :
i + = 1
while j > = 0 and str [j] = = ' ' :
j - = 1
if i > j:
return False
if (i = = j and not ( str [i] > = '0' and
str [i] < = '9' )):
return False
if ( str [i] ! = '.' and str [i] ! = '+' and
str [i] ! = '-' and not ( str [i] > = '0' and
str [i] < = '9' )):
return False
flagDotOrE = False
for i in range (j + 1 ):
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] = = '.' :
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' :
flagDotOrE = True
if ( not ( str [i - 1 ] > = '0' and
str [i - 1 ] < = '9' )):
return False
if i + 1 > len ( str ):
return False
if ( str [i + 1 ] ! = '+' and str [i + 1 ] ! = '-' and
( str [i + 1 ] > = '0' and str [i] < = '9' )):
return False
return True
if __name__ = = '__main__' :
str = "0.1e10"
if valid_number( str ):
print ( 'true' )
else :
print ( 'false' )
|
C#
using System;
class GFG {
public Boolean isValidNumeric(String str)
{
str = str.Trim();
if (str.Length == 0)
return false ;
if (str.Length == 1 && ! char .IsDigit(str[0]))
return false ;
if (str[0] != '+' && str[0] != '-'
&& ! char .IsDigit(str[0]) && str[0] != '.' )
return false ;
Boolean flagDotOrE = false ;
for ( int i = 1; i < str.Length; i++) {
if (! char .IsDigit(str[i]) && str[i] != 'e'
&& str[i] != '.' && str[i] != '+'
&& str[i] != '-' )
return false ;
if (str[i] == '.' ) {
if (flagDotOrE == true )
return false ;
if (i + 1 >= str.Length)
return false ;
if (! char .IsDigit(str[i + 1]))
return false ;
}
else if (str[i] == 'e' ) {
flagDotOrE = true ;
if (! char .IsDigit(str[i - 1]))
return false ;
if (i + 1 >= str.Length)
return false ;
if (! char .IsDigit(str[i + 1]) && str[i + 1] != '+'
&& str[i + 1] != '-' )
return false ;
}
}
return true ;
}
public static void Main(String[] args)
{
String input = "0.1e10" ;
GFG gfg = new GFG();
Console.WriteLine(gfg.isValidNumeric(input));
}
}
|
Javascript
function valid_number(str) {
let i = 0;
let j = str.length - 1;
while (i < str.length && str[i] == ' ' ) {
i++;
}
while (j >= 0 && str[j] == ' ' ) {
j--;
}
if (i > j) {
return false ;
}
if (i == j && !(str[i] >= '0' && str[i] <= '9' )) {
return false ;
}
if (
str[i] != '.' &&
str[i] != '+' &&
str[i] != '-' &&
!(str[i] >= '0' && str[i] <= '9' )
) {
return false ;
}
let flagDotOrE = false ;
for (let k = 0; k <= j; k++) {
if (
str[k] != 'e' &&
str[k] != '.' &&
str[k] != '+' &&
str[k] != '-' &&
!(str[k] >= '0' && str[k] <= '9' )
) {
return false ;
}
if (str[k] == '.' ) {
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' ) {
flagDotOrE = true ;
if (!(str[k - 1] >= '0' && str[k - 1] <= '9' )) {
return false ;
}
if (k + 1 > str.length) {
return false ;
}
if (
str[k + 1] != '+' &&
str[k + 1] != '-' &&
!(str[k + 1] >= '0' && str[k + 1] <= '9' )
) {
return false ;
}
}
}
return true ;
}
let str = "0.1e10" ;
if (valid_number(str)) {
console.log( "true" );
} else {
console.log( "false" );
}
|
Output:
true
Time Complexity: O(n) If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!