C programming question:
Write a function char isValid (char c), return a with a char value of 1 if it is a valid char, and return 0 if it is invalid. (DO NOT USE POINTER!)
valid char are '1' '2' 3' '4' '5' 'A' 'B' 'C'
for example:
lower case is not valid
'1' is valid
'A' is valid
'B' is valid
'ABC' is not valid
'D' is not valid
'9' is not valid
'1234' is not valid
For Testing: make a user input and then put into the function, the program should have loop if the user does not input valid char. (write it into main function!)
#include <stdio.h> char isValid (char c){ if(c=='1' || c=='2' || c=='3' || c=='4' || c=='5' || c=='A' || c=='B' || c=='C'){ return '1'; } else{ return '0'; } } int main(){ char c, tmp; printf("Enter valid char: "); scanf("%c",&c); scanf("%c",&tmp); while(isValid(c)=='0'){ printf("%c is not a valid character\n",c); printf("Please enter valid char: "); scanf("%c",&c); scanf("%c",&tmp); } printf("%c is a valid character\n",c); return 0; }
Get Answers For Free
Most questions answered within 1 hours.