C++ Exercises: Check if a given string is a decimal number or not
C++ Math: Exercise-21 with Solution
In algebra, a decimal number can be defined as a number whose whole number part and the fractional part is separated by a decimal point. Write a C++ program to check if a given string is a decimal number or not.
List of characters of a valid decimal number:
Numbers: 0-9
Positive/negative sign - "+"/"-"
Decimal point - "."
Exponent - "e"
Sample Input: s = 9
Sample Output: Is 0 a decimal number? 1
Sample Input: s = abc 123
Sample Output: Is abc 123 a decimal number? 0
Sample Solution:
C++ Code :
#include <iostream>
using namespace std;
bool isNumber(string str_num) {
int i=0;
int str_len = str_num.size();
while (i<str_len && str_num[i]==' ')
i++;
if (i<str_len && (str_num[i]=='+' || str_num[i]=='-'))
i++;
int digits=0, dots=0;
while (i<str_len && ((str_num[i]>='0' && str_num[i]<='9')||(str_num[i]=='.'))) {
if (str_num[i]>='0' && str_num[i]<='9')
digits++;
else if (str_num[i] == '.')
dots++;
i++;
}
if (digits == 0 || dots>1)
return false;
if (i<str_len && str_num[i]=='e') {
if (++i<str_len && (str_num[i]=='+' || str_num[i]=='-'))
i++;
if (i==str_len || (str_num[i]<'0' || str_num[i]>'9'))
return false;
while (i<str_len && (str_num[i]>='0' && str_num[i]<='9'))
i++;
}
while (i<str_len && str_num[i]==' ') //optional trailing spaces
i++;
return (i==str_len); //if at end of string then success
}
int main() {
string s = "0";
cout << "\n Is " << s << " a decimal number? " << isNumber(s) << endl;
s = "abc 123";
cout << "\n Is " << s << " a decimal number? " << isNumber(s) << endl;
s = "abc";
cout << "\n Is " << s << " a decimal number? " << isNumber(s);
s = "0.12";
cout << "\n Is " << s << " a decimal number? " << isNumber(s) << endl;
s = "123.33";
cout << "\n Is " << s << " a decimal number? " << isNumber(s) << endl;
s = "76.4e93";
cout << "\n Is " << s << " a decimal number? " << isNumber(s) << endl;
s = "+123";
cout << "\n Is " << s << " a decimal number? " << isNumber(s) << endl;
s = "+-33";
cout << "\n Is " << s << " a decimal number? " << isNumber(s) << endl;
return 0;
}
Sample Output:
Is 0 a decimal number? 1 Is abc 123 a decimal number? 0 Is abc a decimal number? 0 Is 0.12 a decimal number? 1 Is 123.33 a decimal number? 1 Is 76.4e93 a decimal number? 1 Is +123 a decimal number? 1 Is +-33 a decimal number? 0
Flowchart:
C++ Code Editor:
Contribute your code and comments through Disqus.
Previous: Write a C++ program to calculate the product of two positive integers represented as strings.
Next: Write a C++ program to compute the sum of two given binary strings. Return result will be a binary string and input strings should not be blank and contains only 1 or 0 charcters.What is the difficulty level of this exercise?