123456789101112131415161718192021
int main () { char buffer[256]; fgets(buffer, 256, stdin); for (int i=0; i<21; ++i) { if (buffer[i]!=' ' || buffer[i]!='-' || buffer[i]!='/' || buffer[i]!='x' || buffer[i]!='X' || buffer[i]!='0' || buffer[i]!='1' || buffer[i]!='2' || buffer[i]!='3' || buffer[i]!='4' || buffer[i]!='5' || buffer[i]!='6' || buffer[i]!='7' || buffer[i]!='8' || buffer[i]!='9') { goto LOOPOUT; } } printf("%s\n",buffer); LOOPOUT: printf("Wrong Character\n"); return 0; }
12
if (buffer != 'A' || buffer != 'B') // do sometinhg
int main () { char buffer[256]; fgets(buffer, 256, stdin); for (int i=0; i<21; ++i) { if (buffer[i]!=' ' && buffer[i]!='-' && buffer[i]!='/' && buffer[i]!='x' && buffer[i]!='X' && buffer[i]!='0' && buffer[i]!='1' && buffer[i]!='2' && buffer[i]!='3' && buffer[i]!='4' && buffer[i]!='5' && buffer[i]!='6' && buffer[i]!='7' && buffer[i]!='8' && buffer[i]!='9') { goto LOOPOUT; } } printf("%s\n",buffer); LOOPOUT: printf("Wrong Character\n"); return 0; }
123456789101112131415161718192021222324
#include <stdio.h> #include <string.h> #include <ctype.h> int main() { enum { NCHARS = 21 }; char buffer[ NCHARS+1 ] = "" ; fgets( buffer, NCHARS+1, stdin ) ; const char allowed[] = " -/xX" ; for( int i = 0 ; i < NCHARS ; ++i ) if( strchr( allowed, buffer[i] ) == NULL && !isdigit( buffer[i] ) ) goto LOOPOUT ; // http://en.cppreference.com/w/c/string/byte/strchr // http://en.cppreference.com/w/c/string/byte/isdigit puts( buffer ) ; return 0 ; LOOPOUT: puts( "Wrong Character" ) ; return 1 ; }