#include <stdio.h>
#include <string.h>
#include <ctype.h>
// This is the function that makes the integer test
bool IsInteger( char *number )
{
int len = strlen( number );
bool isnumber = true;
int i = 0;
if( len == 0 || len > 9 ) // if the user did not enter an input
{ // or if the input is too big to be
return false; // a "long" value.
}
while( i < len && isnumber ) // scanning the the input
{
if( isdigit( number[i] ) == 0 ) // check if there is a none digit character in the input
{
if( number[i] == '+' || number[i] == '-' ) // if so we verify if it is "+" or "-"
{
if( i + 1 > len - 1 || i - 1 >= 0 ) // check the position of "+" or "-"
{ // this signs could only be infront of
isnumber = false; // the number,no where else.
}
}
if( number[i] != '+' && number[i] != '-' ) // if it's not "+" or "-" than
{ // the expression is not a number
isnumber = false;
}
}
i++;
}
return isnumber;
}
void main()
{
char number[20]; // buffer for storing number
printf("Enter a number: "); // geting a number from the user
gets(number);
if( IsInteger( number ) == false ) // testing the input number
{
printf("This is not an integer.\nOr maybe this expression is too big "
"to be tested.\n");
}
else
{
printf("Thanks for entering an integer !\n");
}