>How if i want to make the code as the nric number input must be 14 digits only and not less or more, and include the '-' symbol.'
This would be a good start:
Code:
#include <string.h>
int valid_nric ( const char *nric )
{
size_t len = strlen ( nric );
size_t match = strspn ( nric, "0123456789-" );
if ( len != 14 || match != 14 )
return 0;
return 1;
}
However, if the nric must have a specific number of digits separated by '-', for example, six digits followed by one dash followed by two digits followed by one dash followed by four digits, the above code is too relaxed. You would need to do a little bit more, though not much. Using the above function, a more restrictive solution is clear:
Code:
int valid_nric ( const char *nric )
{
size_t len = strlen ( nric );
size_t match = strspn ( nric, "0123456789-" );
if ( len != 14 || match != 14 )
return 0;
/*
* If it isn't a digit, it must be a dash */
*/
match = strspn ( nric, "0123456789" );
if ( match != 6 )
return 0;
nric += match + 1;
match = strspn ( nric, "0123456789" );
if ( match != 2 )
return 0;
nric += match + 1;
match = strspn ( nric, "0123456789" );
if ( match != 4 )
return 0;
return 1;
}
And it is a good idea to avoid using tabs in your code. If you must use tabs, convert them to spaces if your editor supports that feature. Tabs are evil.