Linkedin Interview Question
Software Engineer / DevelopersCountry: United States
Interview Type: Phone Interview
<pre lang="" line="1" title="CodeMonkey52161" class="run-this">/* Assuming the string is ascii and would denote only +ve numbers.
If negative numbers are allowed, we'd need an additional step to check if first character of the string is a '-' */
class NumCheck{
public static boolean isNumber(String str)
{
boolean isNum=true;
for (int i = 0; i < str.length(); i++){
char c = str.charAt(i);
if(!(c>47 && c<58)) //checking if it lies in the ascii range for numbers
{
isNum= false;
break;
}
}
return isNum;
}
public static void main(String[] args)
{
System.out.println(isNumber("256")?"yes":"no");
System.out.println(isNumber("2fifty6")?"yes":"no");
}
}
</pre><pre title="CodeMonkey52161" input="yes">
</pre>
boolean isNumber = false;
try {
Double.parseDouble(inputStr)
isNumber = true;
} catch (NumberFormatException e) {
}
bool validateNum(string in)
{
bool decimalFound = false, numFound = false;
int i=-1;
if(in[0]=='+' || in[0]=='-') i = 1;
else i=0;
for(; i<in.length(); i++)
{
switch(in[i])
{
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9': numFound = true;
break;
case '.':
if(decimalFound) return false;
else if(numFound) decimalFound = true;
else return false;
break;
default:
return false;
}
}
return true;
}
Using explicit state variable makes it easier to extend the syntax covered.
This program covers octal, decimal, and hex integer numbers and both decimal and hex floating point number (without exponent).
#include <iostream>
#include <sstream>
#include <iomanip>
using namespace std;
enum State {
s_start,
s_sign,
s_0,
s_0x,
s_octal,
s_decimal,
s_hex,
s_decdot,
s_hexdot,
s_nan,
};
bool isNumber(string & str)
{
State state = s_start;
for (int i = 0; i < str.length(); i++) {
char c = str[i];
State next_state = s_nan;
switch(state) {
case s_start:
if (('+' == c) || ('-' == c)) {
next_state = s_sign;
} else if ('0' == c) {
next_state = s_0;
} else if (('1' <= c) && (c <= '9')) {
next_state = s_decimal;
}
break;
case s_sign:
if ('0' == c) {
next_state = s_octal;
} else if (('1' <= c) && (c <= '9')) {
next_state = s_decimal;
}
break;
case s_0:
if (('x' == c) || ('X' == c)) {
next_state = s_0x;
} else if (('0' <= c) && (c <= '7')) {
next_state = s_octal;
} else if ('.' == c) {
next_state = s_decdot;
}
break;
case s_octal:
if (('0' <= c) && (c <= '7')) {
next_state = s_octal;
}
break;
case s_decimal:
if ('.' == c) {
next_state = s_decdot;
break;
}
// Fallthrough
case s_decdot:
if (('0' <= c) && (c <= '9')) {
next_state = s_decimal;
}
break;
case s_hex:
if ('.' == c) {
next_state = s_hexdot;
break;
}
// Fallthrough
case s_hexdot:
case s_0x:
if ((('0' <= c) && (c <= '9'))
|| (('a' <= c) && (c <= 'f'))
|| (('A' <= c) && (c <= 'F'))) {
next_state = s_hex;
}
break;
case s_nan:
return false;
}
state = next_state;
}
if ((s_octal == state) || (s_decimal == state) || (s_hex == state) || (s_0 == state)) {
return true;
}
return false;
}
#include <cstdlib>
int main(int argc, char *argv[])
{
const char *s = "";
if (argc > 1) {
s = argv[1];
}
string str(s);
bool isanum = isNumber(str);
cout << "'" << str << "'" << " is" << (isanum ? "" : " not") << " a number" << endl;
return (0);
}
Minor correction. Adding decimal_point_seen simplifies the program a little. s_decimaldot, s_0dot, and s_hexdot are added to enforce having at least one digit after the decimal point of decimal and hex floating point numbers.
#include <iostream>
#include <sstream>
#include <iomanip>
using namespace std;
enum State {
s_start,
s_sign,
s_0,
s_0x,
s_0dot,
s_octal,
s_decimal,
s_decimaldot,
s_hex,
s_hexdot,
s_nan,
};
bool isNumber(string & str)
{
bool decimal_point_seen = false;
State state = s_start;
for (int i = 0; i < str.length(); i++) {
char c = str[i];
State next_state = s_nan;
switch(state) {
case s_start:
if (('+' == c) || ('-' == c)) {
next_state = s_sign;
} else if ('0' == c) {
next_state = s_0;
} else if (('1' <= c) && (c <= '9')) {
next_state = s_decimal;
}
break;
case s_sign:
if ('0' == c) {
next_state = s_octal;
} else if (('1' <= c) && (c <= '9')) {
next_state = s_decimal;
}
break;
case s_0:
if (('x' == c) || ('X' == c)) {
next_state = s_0x;
} else if (('0' <= c) && (c <= '7')) {
next_state = s_octal;
} else if ('.' == c) {
decimal_point_seen = true;
next_state = s_0dot;
}
break;
case s_octal:
if (('0' <= c) && (c <= '7')) {
next_state = s_octal;
}
break;
case s_decimal:
case s_0dot:
case s_decimaldot:
if (('0' <= c) && (c <= '9')) {
next_state = s_decimal;
} else if ('.' == c) {
if (! decimal_point_seen) {
decimal_point_seen = true;
next_state = s_decimaldot;
}
}
break;
case s_hex:
case s_hexdot:
if ('.' == c) {
if (! decimal_point_seen) {
decimal_point_seen = true;
next_state = s_hexdot;
}
break;
}
// Fallthrough
case s_0x:
if ((('0' <= c) && (c <= '9'))
|| (('a' <= c) && (c <= 'f'))
|| (('A' <= c) && (c <= 'F'))) {
next_state = s_hex;
}
break;
case s_nan:
return false;
}
state = next_state;
}
if ((s_octal == state) || (s_decimal == state) || (s_hex == state) || (s_0 == state)) {
return true;
}
return false;
}
#include <cstdlib>
int main(int argc, char *argv[])
{
const char *s = "";
if (argc > 1) {
s = argv[1];
}
string str(s);
bool isanum = isNumber(str);
cout << "'" << str << "'" << " is" << (isanum ? "" : " not") << " a number" << endl;
return (0);
}
- coder January 25, 2012