home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C by Discovery (4th Edition)
/
C_By_Discovery_4th_Edition.tar
/
C_By_Discovery_4th_Edition
/
_DISK_
/
ch5
/
identchk.c
< prev
next >
Wrap
C/C++ Source or Header
|
2005-06-16
|
2KB
|
57 lines
/* identchk.c
*
* Synopsis - Prompts for and accepts input of a potential
* C identifier and reports whether the identifier
* is legal or not.
*
* Objective - To illustrate a use of strspn().
*/
/* Header Files */
#include <stdio.h>
#include <string.h>
/* Constant Definitions */
#define ID_LGTH 80
#define FALSE 0
#define TRUE 1
/* Function Prototypes */
int id_legal( char id[] );
/* PRECONDITION: id is a null-terminated string.
* POSTCONDITION: returns TRUE if id is a legal C identifier and
* FALSE if not.
*/
int main()
{
char identifier[ID_LGTH];
int retval;
printf( "Enter your identifier: ");
fgets( identifier, ID_LGTH, stdin );
identifier[ strlen( identifier ) -1 ] = '\0'; /* Note 1 */
retval = id_legal( identifier );
if ( retval == TRUE )
printf( "The identifier %s is legal\n", identifier );
else
printf( "%s is not a legal identifer.\n", identifier );
return 0;
}
int id_legal( char id[] )
{
char *legal = "abcdefghijklmnopqrstuvwxyz" /* Note 2 */
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"0123456789_";
char *start = "abcdefghijklmnopqrstuvwxyz" /* Note 3 */
"_ABCDEFGHIJKLMNOPQRSTUVWXYZ";
/* Note 4 */
if ( strspn( id, start) >= 1 && strspn( id, legal ) == strlen( id ) )
return TRUE;
return FALSE;
}