Saturday, 11 April 2015

Checking the Char is digit or not

The following program is used to check whether char is digit or not

void setup(){
Serial.begin(9600);
Serial.println("checking whether char is digit or not");
}

void loop(){
Serial.println("enter character or number");
while(!Serial.available()){
//wait until user enters the data
}//end of while

if(Serial.available()){
char ch = Serial.read();
Serial.println("the character");
if((ch = '0')){
Serial.println("you entered a number ");
Serial.print("the Number is \t");
Serial.print(ch - '0');
Serial.print("\n");
}else{
Serial.println("you entered character");
Serial.print("the character you entered is\t");
Serial.print(ch);
Serial.println("");
}//end of If-Else condition
}//end of If condition
}//end of loop


alternatively we can use isdigit() function from C reference.



isdigit
int isdigit ( int c );
Check if character is decimal digit
Checks whether c is a decimal digit character.

Decimal digits are any of: 0 1 2 3 4 5 6 7 8 9

No comments:

Post a Comment