Same code, minor change to skip spaces. Same end result:
Code:
#include<stdio.h>
#include<ctype.h>
int parse( char *s )
{
if( s )
{
int a = 0, b = 0, c = 0, num = 0;
while( *s && (isalpha( *s ) || ' ' == *s) ) a += (' '!=*s++);
while( *s && (isdigit( *s ) || ' ' == *s) ) b += (' '!=*s++);
while( *s && (isalpha( *s ) || ' ' == *s) ) c += (' '!=*s++);
num = (a * 100) + (b * 10) + c;
switch( num )
{
case 43: /* 1111 AAA */ case 141: /* A 1111 A */
case 142: /* A 1111 AA */ case 241: /* AA 1111 A */
case 242: /* AA 1111 AA */ case 150: /* A 11111 */
case 250: /* AA 11111 */ return 1;
default: return 0;
}
}
return 0;
}
int main( void )
{
char *tests[] =
{
"911aB", "AAA 564", "6114 DFa", "1111Aaa", "aa 11", "GF 1511 SS",
"aa111aa", "aa111a", "aa111aaa", "XX 11111", "a@ 11311", "a 4511",
"a1111"
};
size_t x;
for( x = 0; x < sizeof tests / sizeof tests[0]; x++ )
{
printf("parse( %s ) == %d\n", tests[x], parse( tests[x] ) );
}
return 0;
}
/*
parse( 911aB ) == 0
parse( AAA 564 ) == 0
parse( 6114 DFa ) == 1
parse( 1111Aaa ) == 1
parse( aa 11 ) == 0
parse( GF 1511 SS ) == 1
parse( aa111aa ) == 0
parse( aa111a ) == 0
parse( aa111aaa ) == 0
parse( XX 11111 ) == 1
parse( a@ 11311 ) == 0
parse( a 4511 ) == 0
parse( a1111 ) == 0
*/
Quzah.