Check if a given string is a valid number

Here given code implementation process.

/*
    Java Program for
    Check if a given string is a valid number
*/
public class Numerical
{
	public boolean isValidNumber(String num)
	{
		int n = num.length();
		int i = 0;
		int j = n - 1;
		if (n == 0)
		{
			// When string is empty
			return false;
		}
		// Trim the white space from beginning position.
		while (i < n && num.charAt(i) == ' ')
		{
			i++;
		}
		// Trim the white space from ending position.
		while (j >= 0 && num.charAt(j) == ' ')
		{
			j--;
		}
		if (j < i)
		{
			// When string contain white space only
			return false;
		}
		if (i == j && !(num.charAt(i) >= '0' && 
                        num.charAt(i) <= '9'))
		{
			// When string contains single character. 
			// And this is not a numeric value.
			return false;
		}
		if (num.charAt(i) == '+' || 
            num.charAt(i) == '-' || 
            num.charAt(i) == '.' || 
            num.charAt(i) >= '0' && num.charAt(i) <= '9')
		{
			// When number start with + - and .
			// This three are suitable for start a number.
			if (num.charAt(i) == '-' || num.charAt(i) == '+')
			{
				// + and - at beginning
				i++;
			}
			if (i <= j && num.charAt(i) == '-' || 
                num.charAt(i) == '+')
			{
				if (num.charAt(i) == num.charAt(i - 1))
				{
					// When like this ++123 or --123
					// ++ are used to increment and -- use to decrement
					return false;
				}
				i++;
			}
			boolean flagDot = false;
			boolean flagE = false;
			// Check intermediate of i to j
			while (i < j)
			{
				if (num.charAt(i) >= '0' && num.charAt(i) <= '9')
				{
					// When position of i contain valid numeric digit
					i++;
				}
				else
				{
					if (num.charAt(i) == '.')
					{
						if (flagDot == true || 
                            flagE == true || 
                            (i > 0 && !((num.charAt(i - 1) == ' ') ||
                                        (num.charAt(i - 1) >= '0' && 
                                         num.charAt(i - 1) <= '9'))))
						{
							// Fail case 
							// ➀ When dot appears more than once.
							// ➁ Exponent has no digits.
							return false;
						}
						// When dot is valid
						flagDot = true;
					}
					else if (num.charAt(i) == 'e')
					{
						if (flagE == true || 
                            i + 1 >= n || 
                            !(num.charAt(i - 1) >= '0' && 
                              num.charAt(i - 1) <= '9'))
						{
							// Fail case 
							// ➀ When e appears more than once.
							// ➁ Exponent is last digit in number.
							// ➂ Exponent has previous digit is not number.
							return false;
						}
						if (i + 1 <= j && 
                            (num.charAt(i + 1) == '-' || 
                             num.charAt(i + 1) == '+'))
						{
							i++;
						}
						flagE = true;
					}
					else
					{
						return false;
					}
					i++;
				}
			}
			return true;
		}
		return false;
	}
	public void isNumber(String num)
	{
		if (isValidNumber(num))
		{
			System.out.println(" Given (" + num + ") is valid number");
		}
		else
		{
			System.out.println(" Given (" + num + ") is not valid number");
		}
	}
	public static void main(String[] args)
	{
		Numerical task = new Numerical();
		// 10e2
		task.isNumber("10e2");
		// num = --134
		// Result = no
		task.isNumber("  --134");
		// num = -+123 = -123
		// Result = yes
		task.isNumber("  -+123 ");
		// num = .4 = 0.4
		// Result = yes
		task.isNumber("  .4 ");
		// num = 000.4e-12 = 4e-13
		// Result = yes
		task.isNumber(" 000.4e-12 ");
		// num = 2e1.02
		// Result = No
		task.isNumber(" 2e1.02 ");
	}
}

Output

 Given (10e2) is valid number
 Given (  --134) is not valid number
 Given (  -+123 ) is valid number
 Given (  .4 ) is valid number
 Given ( 000.4e-12 ) is valid number
 Given ( 2e1.02 ) is not valid number
// Include header file
#include <iostream>
#include <string>

using namespace std;
/*
    C++ Program for
    Check if a given string is a valid number
*/
class Numerical
{
	public: bool isValidNumber(string num)
	{
		int n = num.length();
		int i = 0;
		int j = n - 1;
		if (n == 0)
		{
			// When string is empty
			return false;
		}
		// Trim the white space from beginning position.
		while (i < n && num[i] == ' ')
		{
			i++;
		}
		// Trim the white space from ending position.
		while (j >= 0 && num[j] == ' ')
		{
			j--;
		}
		if (j < i)
		{
			// When string contain white space only
			return false;
		}
		if (i == j && !(num[i] >= '0' && num[i] <= '9'))
		{
			// When string contains single character. 
			// And this is not a numeric value.
			return false;
		}
		if (num[i] == '+' || 
            num[i] == '-' || 
            num[i] == '.' || 
            num[i] >= '0' && num[i] <= '9')
		{
			// When number start with + - and .
			// This three are suitable for start a number.
			if (num[i] == '-' || num[i] == '+')
			{
				// + and - at beginning
				i++;
			}
			if (i <= j && num[i] == '-' || num[i] == '+')
			{
				if (num[i] == num[i - 1])
				{
					// When like this ++123 or --123
					// ++ are used to increment and -- use to decrement
					return false;
				}
				i++;
			}
			bool flagDot = false;
			bool flagE = false;
			// Check intermediate of i to j
			while (i < j)
			{
				if (num[i] >= '0' && num[i] <= '9')
				{
					// When position of i contain valid numeric digit
					i++;
				}
				else
				{
					if (num[i] == '.')
					{
						if (flagDot == true || 
                            flagE == true || 
                            (i > 0 && !((num[i - 1] == ' ') || 
                                        (num[i - 1] >= '0' && 
                                         num[i - 1] <= '9'))))
						{
							// Fail case 
							// ➀ When dot appears more than once.
							// ➁ Exponent has no digits.
							return false;
						}
						// When dot is valid
						flagDot = true;
					}
					else if (num[i] == 'e')
					{
						if (flagE == true || 
                            i + 1 >= n || 
                            !(num[i - 1] >= '0' && 
                              num[i - 1] <= '9'))
						{
							// Fail case 
							// ➀ When e appears more than once.
							// ➁ Exponent is last digit in number.
							// ➂ Exponent has previous digit is not number.
							return false;
						}
						if (i + 1 <= j && 
                            (num[i + 1] == '-' || num[i + 1] == '+'))
						{
							i++;
						}
						flagE = true;
					}
					else
					{
						return false;
					}
					i++;
				}
			}
			return true;
		}
		return false;
	}
	void isNumber(string num)
	{
		if (this->isValidNumber(num))
		{
			cout << " Given (" << num << ") is valid number" << endl;
		}
		else
		{
			cout << " Given (" << num << ") is not valid number" << endl;
		}
	}
};
int main()
{
	Numerical *task = new Numerical();
	// 10e2
	task->isNumber("10e2");
	// num = --134
	// Result = no
	task->isNumber("  --134");
	// num = -+123 = -123
	// Result = yes
	task->isNumber("  -+123 ");
	// num = .4 = 0.4
	// Result = yes
	task->isNumber("  .4 ");
	// num = 000.4e-12 = 4e-13
	// Result = yes
	task->isNumber(" 000.4e-12 ");
	// num = 2e1.02
	// Result = No
	task->isNumber(" 2e1.02 ");
	return 0;
}

Output

 Given (10e2) is valid number
 Given (  --134) is not valid number
 Given (  -+123 ) is valid number
 Given (  .4 ) is valid number
 Given ( 000.4e-12 ) is valid number
 Given ( 2e1.02 ) is not valid number
// Include namespace system
using System;
/*
    Csharp Program for
    Check if a given string is a valid number
*/
public class Numerical
{
	public Boolean isValidNumber(String num)
	{
		int n = num.Length;
		int i = 0;
		int j = n - 1;
		if (n == 0)
		{
			// When string is empty
			return false;
		}
		// Trim the white space from beginning position.
		while (i < n && num[i] == ' ')
		{
			i++;
		}
		// Trim the white space from ending position.
		while (j >= 0 && num[j] == ' ')
		{
			j--;
		}
		if (j < i)
		{
			// When string contain white space only
			return false;
		}
		if (i == j && !(num[i] >= '0' && num[i] <= '9'))
		{
			// When string contains single character. 
			// And this is not a numeric value.
			return false;
		}
		if (num[i] == '+' || 
            num[i] == '-' || 
            num[i] == '.' || 
            num[i] >= '0' && num[i] <= '9')
		{
			// When number start with + - and .
			// This three are suitable for start a number.
			if (num[i] == '-' || num[i] == '+')
			{
				// + and - at beginning
				i++;
			}
			if (i <= j && num[i] == '-' || num[i] == '+')
			{
				if (num[i] == num[i - 1])
				{
					// When like this ++123 or --123
					// ++ are used to increment and -- use to decrement
					return false;
				}
				i++;
			}
			Boolean flagDot = false;
			Boolean flagE = false;
			// Check intermediate of i to j
			while (i < j)
			{
				if (num[i] >= '0' && num[i] <= '9')
				{
					// When position of i contain valid numeric digit
					i++;
				}
				else
				{
					if (num[i] == '.')
					{
						if (flagDot == true || 
                            flagE == true || 
                            (i > 0 && 
                             !((num[i - 1] == ' ') || 
                               (num[i - 1] >= '0' && num[i - 1] <= '9'))))
						{
							// Fail case 
							// ➀ When dot appears more than once.
							// ➁ Exponent has no digits.
							return false;
						}
						// When dot is valid
						flagDot = true;
					}
					else if (num[i] == 'e')
					{
						if (flagE == true || 
                            i + 1 >= n || 
                            !(num[i - 1] >= '0' && num[i - 1] <= '9'))
						{
							// Fail case 
							// ➀ When e appears more than once.
							// ➁ Exponent is last digit in number.
							// ➂ Exponent has previous digit is not number.
							return false;
						}
						if (i + 1 <= j && 
                            (num[i + 1] == '-' || num[i + 1] == '+'))
						{
							i++;
						}
						flagE = true;
					}
					else
					{
						return false;
					}
					i++;
				}
			}
			return true;
		}
		return false;
	}
	public void isNumber(String num)
	{
		if (this.isValidNumber(num))
		{
			Console.WriteLine(" Given (" + num + ") is valid number");
		}
		else
		{
			Console.WriteLine(" Given (" + num + ") is not valid number");
		}
	}
	public static void Main(String[] args)
	{
		Numerical task = new Numerical();
		// 10e2
		task.isNumber("10e2");
		// num = --134
		// Result = no
		task.isNumber("  --134");
		// num = -+123 = -123
		// Result = yes
		task.isNumber("  -+123 ");
		// num = .4 = 0.4
		// Result = yes
		task.isNumber("  .4 ");
		// num = 000.4e-12 = 4e-13
		// Result = yes
		task.isNumber(" 000.4e-12 ");
		// num = 2e1.02
		// Result = No
		task.isNumber(" 2e1.02 ");
	}
}

Output

 Given (10e2) is valid number
 Given (  --134) is not valid number
 Given (  -+123 ) is valid number
 Given (  .4 ) is valid number
 Given ( 000.4e-12 ) is valid number
 Given ( 2e1.02 ) is not valid number
package main
import "fmt"
/*
    Go Program for
    Check if a given string is a valid number
*/

func isValidNumber(num string) bool {
	var n int = len(num)
	var i int = 0
	var j int = n - 1
	if n == 0 {
		// When string is empty
		return false
	}
	// Trim the white space from beginning position.
	for (i < n && num[i] == ' ') {
		i++
	}
	// Trim the white space from ending position.
	for (j >= 0 && num[j] == ' ') {
		j--
	}
	if j < i {
		// When string contain white space only
		return false
	}
	if i == j && !(num[i] >= '0' && num[i] <= '9') {
		// When string contains single character. 
		// And this is not a numeric value.
		return false
	}
	if num[i] == '+' || num[i] == '-' || 
		num[i] == '.' || num[i] >= '0' && num[i] <= '9' {
		// When number start with + - and .
		// This three are suitable for start a number.
		if num[i] == '-' || num[i] == '+' {
			// + and - at beginning
			i++
		}
		if i <= j && num[i] == '-' || num[i] == '+' {
			if num[i] == num[i - 1] {
				// When like this ++123 or --123
				// ++ are used to increment and -- use to decrement
				return false
			}
			i++
		}
		var flagDot bool = false
		var flagE bool = false
		// Check intermediate of i to j
		for (i < j) {
			if num[i] >= '0' && num[i] <= '9' {
				// When position of i contain valid numeric digit
				i++
			} else {
				if num[i] == '.' {
					if flagDot == true || flagE == true || 
						(i > 0 && !((num[i - 1] == ' ') || 
							(num[i - 1] >= '0' && num[i - 1] <= '9'))) {
						// Fail case 
						// ➀ When dot appears more than once.
						// ➁ Exponent has no digits.
						return false
					}
					// When dot is valid
					flagDot = true
				} else if num[i] == 'e' {
					if flagE == true || i + 1 >= n || 
						!(num[i - 1] >= '0' && num[i - 1] <= '9') {
						// Fail case 
						// ➀ When e appears more than once.
						// ➁ Exponent is last digit in number.
						// ➂ Exponent has previous digit is not number.
						return false
					}
					if i + 1 <= j && (num[i + 1] == '-' || num[i + 1] == '+') {
						i++
					}
					flagE = true
				} else {
					return false
				}
				i++
			}
		}
		return true
	}
	return false
}
func isNumber(num string) {
	if isValidNumber(num) {
		fmt.Println(" Given (", num, ") is valid number")
	} else {
		fmt.Println(" Given (", num, ") is not valid number")
	}
}
func main() {

	// 10e2
	isNumber("10e2")
	// num = --134
	// Result = no
	isNumber("  --134")
	// num = -+123 = -123
	// Result = yes
	isNumber("  -+123 ")
	// num = .4 = 0.4
	// Result = yes
	isNumber("  .4 ")
	// num = 000.4e-12 = 4e-13
	// Result = yes
	isNumber(" 000.4e-12 ")
	// num = 2e1.02
	// Result = No
	isNumber(" 2e1.02 ")
}

Output

 Given (10e2) is valid number
 Given (  --134) is not valid number
 Given (  -+123 ) is valid number
 Given (  .4 ) is valid number
 Given ( 000.4e-12 ) is valid number
 Given ( 2e1.02 ) is not valid number
<?php
/*
    Php Program for
    Check if a given string is a valid number
*/
class Numerical
{
	public function isValidNumber($num)
	{
		$n = strlen($num);
		$i = 0;
		$j = $n - 1;
		if ($n == 0)
		{
			// When string is empty
			return false;
		}
		// Trim the white space from beginning position.
		while ($i < $n && $num[$i] == ' ')
		{
			$i++;
		}
		// Trim the white space from ending position.
		while ($j >= 0 && $num[$j] == ' ')
		{
			$j--;
		}
		if ($j < $i)
		{
			// When string contain white space only
			return false;
		}
		if ($i == $j && !($num[$i] >= '0' && $num[$i] <= '9'))
		{
			// When string contains single character. 
			// And this is not a numeric value.
			return false;
		}
		if ($num[$i] == '+' || $num[$i] == '-' || 
            $num[$i] == '.' || $num[$i] >= '0' && $num[$i] <= '9')
		{
			// When number start with + - and .
			// This three are suitable for start a number.
			if ($num[$i] == '-' || $num[$i] == '+')
			{
				// + and - at beginning
				$i++;
			}
			if ($i <= $j && $num[$i] == '-' || $num[$i] == '+')
			{
				if ($num[$i] == $num[$i - 1])
				{
					// When like this ++123 or --123
					// ++ are used to increment and -- use to decrement
					return false;
				}
				$i++;
			}
			$flagDot = false;
			$flagE = false;
			// Check intermediate of i to j
			while ($i < $j)
			{
				if ($num[$i] >= '0' && $num[$i] <= '9')
				{
					// When position of i contain valid numeric digit
					$i++;
				}
				else
				{
					if ($num[$i] == '.')
					{
						if ($flagDot == true || 
                            $flagE == true || 
                            ($i > 0 && 
                             !(($num[$i - 1] == ' ') || 
                               ($num[$i - 1] >= '0' && 
                                $num[$i - 1] <= '9'))))
						{
							// Fail case 
							// ➀ When dot appears more than once.
							// ➁ Exponent has no digits.
							return false;
						}
						// When dot is valid
						$flagDot = true;
					}
					else if ($num[$i] == 'e')
					{
						if ($flagE == true || 
                            $i + 1 >= $n || 
                            !($num[$i - 1] >= '0' && 
                              $num[$i - 1] <= '9'))
						{
							// Fail case 
							// ➀ When e appears more than once.
							// ➁ Exponent is last digit in number.
							// ➂ Exponent has previous digit is not number.
							return false;
						}
						if ($i + 1 <= $j && 
                            ($num[$i + 1] == '-' || 
                             $num[$i + 1] == '+'))
						{
							$i++;
						}
						$flagE = true;
					}
					else
					{
						return false;
					}
					$i++;
				}
			}
			return true;
		}
		return false;
	}
	public	function isNumber($num)
	{
		if ($this->isValidNumber($num))
		{
			echo(" Given (".$num.") is valid number"."\n");
		}
		else
		{
			echo(" Given (".$num.") is not valid number"."\n");
		}
	}
}

function main()
{
	$task = new Numerical();
	// 10e2
	$task->isNumber("10e2");
	// num = --134
	// Result = no
	$task->isNumber("  --134");
	// num = -+123 = -123
	// Result = yes
	$task->isNumber("  -+123 ");
	// num = .4 = 0.4
	// Result = yes
	$task->isNumber("  .4 ");
	// num = 000.4e-12 = 4e-13
	// Result = yes
	$task->isNumber(" 000.4e-12 ");
	// num = 2e1.02
	// Result = No
	$task->isNumber(" 2e1.02 ");
}
main();

Output

 Given (10e2) is valid number
 Given (  --134) is not valid number
 Given (  -+123 ) is valid number
 Given (  .4 ) is valid number
 Given ( 000.4e-12 ) is valid number
 Given ( 2e1.02 ) is not valid number
/*
    Node JS Program for
    Check if a given string is a valid number
*/
class Numerical
{
	isValidNumber(num)
	{
		var n = num.length;
		var i = 0;
		var j = n - 1;
		if (n == 0)
		{
			// When string is empty
			return false;
		}
		// Trim the white space from beginning position.
		while (i < n && num.charAt(i) == ' ')
		{
			i++;
		}
		// Trim the white space from ending position.
		while (j >= 0 && num.charAt(j) == ' ')
		{
			j--;
		}
		if (j < i)
		{
			// When string contain white space only
			return false;
		}
		if (i == j && !(num.charAt(i) >= '0' && num.charAt(i) <= '9'))
		{
			// When string contains single character. 
			// And this is not a numeric value.
			return false;
		}
		if (num.charAt(i) == '+' || 
            num.charAt(i) == '-' ||
            num.charAt(i) == '.' || 
            num.charAt(i) >= '0' && 
            num.charAt(i) <= '9')
		{
			// When number start with + - and .
			// This three are suitable for start a number.
			if (num.charAt(i) == '-' || num.charAt(i) == '+')
			{
				// + and - at beginning
				i++;
			}
			if (i <= j && num.charAt(i) == '-' || num.charAt(i) == '+')
			{
				if (num.charAt(i) == num.charAt(i - 1))
				{
					// When like this ++123 or --123
					// ++ are used to increment and -- use to decrement
					return false;
				}
				i++;
			}
			var flagDot = false;
			var flagE = false;
			// Check intermediate of i to j
			while (i < j)
			{
				if (num.charAt(i) >= '0' && num.charAt(i) <= '9')
				{
					// When position of i contain valid numeric digit
					i++;
				}
				else
				{
					if (num.charAt(i) == '.')
					{
						if (flagDot == true || 
                            flagE == true || 
                            (i > 0 && !((num.charAt(i - 1) == ' ') ||
                                        (num.charAt(i - 1) >= '0' &&
                                         num.charAt(i - 1) <= '9'))))
						{
							// Fail case 
							// ➀ When dot appears more than once.
							// ➁ Exponent has no digits.
							return false;
						}
						// When dot is valid
						flagDot = true;
					}
					else if (num.charAt(i) == 'e')
					{
						if (flagE == true || 
                            i + 1 >= n || 
                            !(num.charAt(i - 1) >= '0' && 
                              num.charAt(i - 1) <= '9'))
						{
							// Fail case 
							// ➀ When e appears more than once.
							// ➁ Exponent is last digit in number.
							// ➂ Exponent has previous digit is not number.
							return false;
						}
						if (i + 1 <= j && 
                            (num.charAt(i + 1) == '-' || 
                             num.charAt(i + 1) == '+'))
						{
							i++;
						}
						flagE = true;
					}
					else
					{
						return false;
					}
					i++;
				}
			}
			return true;
		}
		return false;
	}
	isNumber(num)
	{
		if (this.isValidNumber(num))
		{
			console.log(" Given (" + num + ") is valid number");
		}
		else
		{
			console.log(" Given (" + num + ") is not valid number");
		}
	}
}

function main()
{
	var task = new Numerical();
	// 10e2
	task.isNumber("10e2");
	// num = --134
	// Result = no
	task.isNumber("  --134");
	// num = -+123 = -123
	// Result = yes
	task.isNumber("  -+123 ");
	// num = .4 = 0.4
	// Result = yes
	task.isNumber("  .4 ");
	// num = 000.4e-12 = 4e-13
	// Result = yes
	task.isNumber(" 000.4e-12 ");
	// num = 2e1.02
	// Result = No
	task.isNumber(" 2e1.02 ");
}
main();

Output

 Given (10e2) is valid number
 Given (  --134) is not valid number
 Given (  -+123 ) is valid number
 Given (  .4 ) is valid number
 Given ( 000.4e-12 ) is valid number
 Given ( 2e1.02 ) is not valid number
#    Python 3 Program for
#    Check if a given string is a valid number
class Numerical :
	def isValidNumber(self, num) :
		n = len(num)
		i = 0
		j = n - 1
		if (n == 0) :
			#  When string is empty
			return False
		
		#  Trim the white space from beginning position.
		while (i < n and num[i] == ' ') :
			i += 1
		
		#  Trim the white space from ending position.
		while (j >= 0 and num[j] == ' ') :
			j -= 1
		
		if (j < i) :
			#  When string contain white space only
			return False
		
		if (i == j and not(num[i] >= '0'
				and num[i] <= '9')) :
			#  When string contains single character. 
			#  And this is not a numeric value.
			return False
		
		if (num[i] == '+'
			or num[i] == '-'
			or num[i] == '.'
			or num[i] >= '0'
			and num[i] <= '9') :
			#  When number start with + - and .
			#  This three are suitable for start a number.
			if (num[i] == '-'
				or num[i] == '+') :
				#  + and - at beginning
				i += 1
			
			if (i <= j and num[i] == '-'
				or num[i] == '+') :
				if (num[i] == num[i - 1]) :
					#  When like this ++123 or --123
					#  ++ are used to increment and -- use to decrement
					return False
				
				i += 1
			
			flagDot = False
			flagE = False
			#  Check intermediate of i to j
			while (i < j) :
				if (num[i] >= '0'
					and num[i] <= '9') :
					#  When position of i contain valid numeric digit
					i += 1
				else :
					if (num[i] == '.') :
						if (flagDot == True or 
                            flagE == True or(i > 0 and 
                            not((num[i - 1] == ' ') or(num[i - 1] >= '0'
								and num[i - 1] <= '9')))) :
							#  Fail case 
							#  ➀ When dot appears more than once.
							#  ➁ Exponent has no digits.
							return False
						
						#  When dot is valid
						flagDot = True
					elif (num[i] == 'e') :
						if (flagE == True or i + 1 >= n or 
                            not(num[i - 1] >= '0'
								and num[i - 1] <= '9')) :
							#  Fail case 
							#  ➀ When e appears more than once.
							#  ➁ Exponent is last digit in number.
							#  ➂ Exponent has previous digit is not number.
							return False
						
						if (i + 1 <= j and(num[i + 1] == '-'
								or num[i + 1] == '+')) :
							i += 1
						
						flagE = True
					else :
						return False
					
					i += 1
				
			
			return True
		
		return False
	
	def isNumber(self, num) :
		if (self.isValidNumber(num)) :
			print(" Given (", num ,") is valid number")
		else :
			print(" Given (", num ,") is not valid number")
		
	

def main() :
	task = Numerical()
	#  10e2
	task.isNumber("10e2")
	#  num = --134
	#  Result = no
	task.isNumber("  --134")
	#  num = -+123 = -123
	#  Result = yes
	task.isNumber("  -+123 ")
	#  num = .4 = 0.4
	#  Result = yes
	task.isNumber("  .4 ")
	#  num = 000.4e-12 = 4e-13
	#  Result = yes
	task.isNumber(" 000.4e-12 ")
	#  num = 2e1.02
	#  Result = No
	task.isNumber(" 2e1.02 ")

if __name__ == "__main__": main()

Output

 Given ( 10e2 ) is valid number
 Given (   --134 ) is not valid number
 Given (   -+123  ) is valid number
 Given (   .4  ) is valid number
 Given (  000.4e-12  ) is valid number
 Given (  2e1.02  ) is not valid number
#    Ruby Program for
#    Check if a given string is a valid number
class Numerical 
	def isValidNumber(num) 
		n = num.length
		i = 0
		j = n - 1
		if (n == 0) 
			#  When string is empty
			return false
		end

		#  Trim the white space from beginning position.
		while (i < n && num[i] == ' ') 
			i += 1
		end

		#  Trim the white space from ending position.
		while (j >= 0 && num[j] == ' ') 
			j -= 1
		end

		if (j < i) 
			#  When string contain white space only
			return false
		end

		if (i == j && !(num[i] >= '0' && num[i] <= '9')) 
			#  When string contains single character. 
			#  And this is not a numeric value.
			return false
		end

		if (num[i] == '+' || 
            num[i] == '-' || 
            num[i] == '.' || 
            num[i] >= '0' && num[i] <= '9') 
			#  When number start with + - and .
			#  This three are suitable for start a number.
			if (num[i] == '-' || num[i] == '+') 
				#  + and - at beginning
				i += 1
			end

			if (i <= j && num[i] == '-' || num[i] == '+') 
				if (num[i] == num[i - 1]) 
					#  When like this ++123 or --123
					#  ++ are used to increment and -- use to decrement
					return false
				end

				i += 1
			end

			flagDot = false
			flagE = false
			#  Check intermediate of i to j
			while (i < j) 
				if (num[i] >= '0' && num[i] <= '9') 
					#  When position of i contain valid numeric digit
					i += 1
				else
 
					if (num[i] == '.') 
						if (flagDot == true || 
                            flagE == true || 
                            (i > 0 && !((num[i - 1] == ' ') || 
                                        (num[i - 1] >= '0' && 
                                         num[i - 1] <= '9')))) 
							#  Fail case 
							#  ➀ When dot appears more than once.
							#  ➁ Exponent has no digits.
							return false
						end

						#  When dot is valid
						flagDot = true
					elsif (num[i] == 'e') 
						if (flagE == true || 
                            i + 1 >= n || 
                            !(num[i - 1] >= '0' && 
                              num[i - 1] <= '9')) 
							#  Fail case 
							#  ➀ When e appears more than once.
							#  ➁ Exponent is last digit in number.
							#  ➂ Exponent has previous digit is not number.
							return false
						end

						if (i + 1 <= j && 
                            (num[i + 1] == '-' || 
                             num[i + 1] == '+')) 
							i += 1
						end

						flagE = true
					else
 
						return false
					end

					i += 1
				end

			end

			return true
		end

		return false
	end

	def isNumber(num) 
		if (self.isValidNumber(num)) 
			print(" Given (", num ,") is valid number", "\n")
		else
 
			print(" Given (", num ,") is not valid number", "\n")
		end

	end

end

def main() 
	task = Numerical.new()
	#  10e2
	task.isNumber("10e2")
	#  num = --134
	#  Result = no
	task.isNumber("  --134")
	#  num = -+123 = -123
	#  Result = yes
	task.isNumber("  -+123 ")
	#  num = .4 = 0.4
	#  Result = yes
	task.isNumber("  .4 ")
	#  num = 000.4e-12 = 4e-13
	#  Result = yes
	task.isNumber(" 000.4e-12 ")
	#  num = 2e1.02
	#  Result = No
	task.isNumber(" 2e1.02 ")
end

main()

Output

 Given (10e2) is valid number
 Given (  --134) is not valid number
 Given (  -+123 ) is valid number
 Given (  .4 ) is valid number
 Given ( 000.4e-12 ) is valid number
 Given ( 2e1.02 ) is not valid number
import scala.collection.mutable._;
/*
    Scala Program for
    Check if a given string is a valid number
*/
class Numerical()
{
	def isValidNumber(num: String): Boolean = {
		var n: Int = num.length();
		var i: Int = 0;
		var j: Int = n - 1;
		if (n == 0)
		{
			// When string is empty
			return false;
		}
		// Trim the white space from beginning position.
		while (i < n && num.charAt(i) == ' ')
		{
			i += 1;
		}
		// Trim the white space from ending position.
		while (j >= 0 && num.charAt(j) == ' ')
		{
			j -= 1;
		}
		if (j < i)
		{
			// When string contain white space only
			return false;
		}
		if (i == j && !(num.charAt(i) >= '0' && num.charAt(i) <= '9'))
		{
			// When string contains single character. 
			// And this is not a numeric value.
			return false;
		}
		if (num.charAt(i) == '+' || 
            num.charAt(i) == '-' || 
            num.charAt(i) == '.' || 
            num.charAt(i) >= '0' && 
            num.charAt(i) <= '9')
		{
			// When number start with + - and .
			// This three are suitable for start a number.
			if (num.charAt(i) == '-' || 
                num.charAt(i) == '+')
			{
				// + and - at beginning
				i += 1;
			}
			if (i <= j && num.charAt(i) == '-' || 
                num.charAt(i) == '+')
			{
				if (num.charAt(i) == num.charAt(i - 1))
				{
					// When like this ++123 or --123
					// ++ are used to increment and -- use to decrement
					return false;
				}
				i += 1;
			}
			var flagDot: Boolean = false;
			var flagE: Boolean = false;
			// Check intermediate of i to j
			while (i < j)
			{
				if (num.charAt(i) >= '0' && num.charAt(i) <= '9')
				{
					// When position of i contain valid numeric digit
					i += 1;
				}
				else
				{
					if (num.charAt(i) == '.')
					{
						if (flagDot == true || 
                            flagE == true || 
                            (i > 0 && !((num.charAt(i - 1) == ' ') ||
                                        (num.charAt(i - 1) >= '0' &&
                                         num.charAt(i - 1) <= '9'))))
						{
							// Fail case 
							// ➀ When dot appears more than once.
							// ➁ Exponent has no digits.
							return false;
						}
						// When dot is valid
						flagDot = true;
					}
					else if (num.charAt(i) == 'e')
					{
						if (flagE == true || i + 1 >= n || 
                            !(num.charAt(i - 1) >= '0' && 
                              num.charAt(i - 1) <= '9'))
						{
							// Fail case 
							// ➀ When e appears more than once.
							// ➁ Exponent is last digit in number.
							// ➂ Exponent has previous digit is not number.
							return false;
						}
						if (i + 1 <= j && 
                            (num.charAt(i + 1) == '-' || 
                             num.charAt(i + 1) == '+'))
						{
							i += 1;
						}
						flagE = true;
					}
					else
					{
						return false;
					}
					i += 1;
				}
			}
			return true;
		}
		return false;
	}
	def isNumber(num: String): Unit = {
		if (isValidNumber(num))
		{
			println(" Given (" + num + ") is valid number");
		}
		else
		{
			println(" Given (" + num + ") is not valid number");
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Numerical = new Numerical();
		// 10e2
		task.isNumber("10e2");
		// num = --134
		// Result = no
		task.isNumber("  --134");
		// num = -+123 = -123
		// Result = yes
		task.isNumber("  -+123 ");
		// num = .4 = 0.4
		// Result = yes
		task.isNumber("  .4 ");
		// num = 000.4e-12 = 4e-13
		// Result = yes
		task.isNumber(" 000.4e-12 ");
		// num = 2e1.02
		// Result = No
		task.isNumber(" 2e1.02 ");
	}
}

Output

 Given (10e2) is valid number
 Given (  --134) is not valid number
 Given (  -+123 ) is valid number
 Given (  .4 ) is valid number
 Given ( 000.4e-12 ) is valid number
 Given ( 2e1.02 ) is not valid number
import Foundation;
/*
    Swift 4 Program for
    Check if a given string is a valid number
*/
class Numerical
{
	func isValidNumber(_ text: String) -> Bool
	{
      	let num = Array(text);
		let n: Int = num.count;
		var i: Int = 0;
		var j: Int = n - 1;
		if (n == 0)
		{
			// When string is empty
			return false;
		}
		// Trim the white space from beginning position.
		while (i < n && num[i] == " ")
		{
			i += 1;
		}
		// Trim the white space from ending position.
		while (j >= 0 && num[j] == " ")
		{
			j -= 1;
		}
		if (j < i)
		{
			// When string contain white space only
			return false;
		}
		if (i == j && !(num[i] >= "0" && num[i] <= "9"))
		{
			// When string contains single character. 
			// And this is not a numeric value.
			return false;
		}
		if (num[i] == "+" || 
            num[i] == "-" || 
            num[i] == "." || 
            num[i] >= "0" && num[i] <= "9")
		{
			// When number start with + - and .
			// This three are suitable for start a number.
			if (num[i] == "-" || num[i] == "+")
			{
				// + and - at beginning
				i += 1;
			}
			if (i <= j && num[i] == "-" || num[i] == "+")
			{
				if (num[i] == num[i - 1])
				{
					// When like this ++123 or --123
					// ++ are used to increment and -- use to decrement
					return false;
				}
				i += 1;
			}
			var flagDot: Bool = false;
			var flagE: Bool = false;
			// Check intermediate of i to j
			while (i < j)
			{
				if (num[i] >= "0" && num[i] <= "9")
				{
					// When position of i contain valid numeric digit
					i += 1;
				}
				else
				{
					if (num[i] == ".")
					{
						if (flagDot == true || 
                            flagE == true || 
                            (i > 0 && !((num[i - 1] == " ") || 
                                        (num[i - 1] >= "0" && 
                                         num[i - 1] <= "9"))))
						{
							// Fail case 
							// ➀ When dot appears more than once.
							// ➁ Exponent has no digits.
							return false;
						}
						// When dot is valid
						flagDot = true;
					}
					else if (num[i] == "e")
					{
						if (flagE == true || 
                            i + 1 >= n || 
                            !(num[i - 1] >= "0" && 
                              num[i - 1] <= "9"))
						{
							// Fail case 
							// ➀ When e appears more than once.
							// ➁ Exponent is last digit in number.
							// ➂ Exponent has previous digit is not number.
							return false;
						}
						if (i + 1 <= j && 
                            (num[i + 1] == "-" || 
                             num[i + 1] == "+"))
						{
							i += 1;
						}
						flagE = true;
					}
					else
					{
						return false;
					}
					i += 1;
				}
			}
			return true;
		}
		return false;
	}
	func isNumber(_ num: String)
	{
		if (self.isValidNumber(num))
		{
			print(" Given (", num ,") is valid number");
		}
		else
		{
			print(" Given (", num ,") is not valid number");
		}
	}
}
func main()
{
	let task: Numerical = Numerical();
	// 10e2
	task.isNumber("10e2");
	// num = --134
	// Result = no
	task.isNumber("  --134");
	// num = -+123 = -123
	// Result = yes
	task.isNumber("  -+123 ");
	// num = .4 = 0.4
	// Result = yes
	task.isNumber("  .4 ");
	// num = 000.4e-12 = 4e-13
	// Result = yes
	task.isNumber(" 000.4e-12 ");
	// num = 2e1.02
	// Result = No
	task.isNumber(" 2e1.02 ");
}
main();

Output

 Given ( 10e2 ) is valid number
 Given (   --134 ) is not valid number
 Given (   -+123  ) is valid number
 Given (   .4  ) is valid number
 Given (  000.4e-12  ) is valid number
 Given (  2e1.02  ) is not valid number
/*
    Kotlin Program for
    Check if a given string is a valid number
*/
class Numerical
{
	fun isValidNumber(num: String): Boolean
	{
		val n: Int = num.length;
		var i: Int = 0;
		var j: Int = n - 1;
		if (n == 0)
		{
			// When string is empty
			return false;
		}
		// Trim the white space from beginning position.
		while (i < n && num.get(i) == ' ')
		{
			i += 1;
		}
		// Trim the white space from ending position.
		while (j >= 0 && num.get(j) == ' ')
		{
			j -= 1;
		}
		if (j < i)
		{
			// When string contain white space only
			return false;
		}
		if (i == j && !(num.get(i) >= '0' && num.get(i) <= '9'))
		{
			// When string contains single character. 
			// And this is not a numeric value.
			return false;
		}
		if (num.get(i) == '+' || 
            num.get(i) == '-' || 
            num.get(i) == '.' || 
            num.get(i) >= '0' && num.get(i) <= '9')
		{
			// When number start with + - and .
			// This three are suitable for start a number.
			if (num.get(i) == '-' || num.get(i) == '+')
			{
				// + and - at beginning
				i += 1;
			}
			if (i <= j && num.get(i) == '-' || num.get(i) == '+')
			{
				if (num.get(i) == num.get(i - 1))
				{
					// When like this ++123 or --123
					// ++ are used to increment and -- use to decrement
					return false;
				}
				i += 1;
			}
			var flagDot: Boolean = false;
			var flagE: Boolean = false;
			// Check intermediate of i to j
			while (i < j)
			{
				if (num.get(i) >= '0' && num.get(i) <= '9')
				{
					// When position of i contain valid numeric digit
					i += 1;
				}
				else
				{
					if (num.get(i) == '.')
					{
						if (flagDot == true || 
                            flagE == true || 
                            (i > 0 && 
                             !((num.get(i - 1) == ' ') || 
                               (num.get(i - 1) >= '0' && 
                                num.get(i - 1) <= '9'))))
						{
							// Fail case 
							// ➀ When dot appears more than once.
							// ➁ Exponent has no digits.
							return false;
						}
						// When dot is valid
						flagDot = true;
					}
					else if (num.get(i) == 'e')
					{
						if (flagE == true || 
                            i + 1 >= n || 
                            !(num.get(i - 1) >= '0' && 
                              num.get(i - 1) <= '9'))
						{
							// Fail case 
							// ➀ When e appears more than once.
							// ➁ Exponent is last digit in number.
							// ➂ Exponent has previous digit is not number.
							return false;
						}
						if (i + 1 <= j && 
                            (num.get(i + 1) == '-' || 
                             num.get(i + 1) == '+'))
						{
							i += 1;
						}
						flagE = true;
					}
					else
					{
						return false;
					}
					i += 1;
				}
			}
			return true;
		}
		return false;
	}
	fun isNumber(num: String): Unit
	{
		if (this.isValidNumber(num))
		{
			println(" Given (" + num + ") is valid number");
		}
		else
		{
			println(" Given (" + num + ") is not valid number");
		}
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Numerical = Numerical();
	// 10e2
	task.isNumber("10e2");
	// num = --134
	// Result = no
	task.isNumber("  --134");
	// num = -+123 = -123
	// Result = yes
	task.isNumber("  -+123 ");
	// num = .4 = 0.4
	// Result = yes
	task.isNumber("  .4 ");
	// num = 000.4e-12 = 4e-13
	// Result = yes
	task.isNumber(" 000.4e-12 ");
	// num = 2e1.02
	// Result = No
	task.isNumber(" 2e1.02 ");
}

Output

 Given (10e2) is valid number
 Given (  --134) is not valid number
 Given (  -+123 ) is valid number
 Given (  .4 ) is valid number
 Given ( 000.4e-12 ) is valid number
 Given ( 2e1.02 ) is not valid number


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved