/*
* Name: identifier.c
* Author: x-bit, at www.patching.net, AllRights Reserved.
* Des cription: 输入一串字符,判定该字符串是不是合法的C语言标识符。
* Date: 09-05-2004
* Compile environment: win2k+vc6下调试通过
*
*/
#include <stdio.h>
int identf(char *str);
int isKey(char *str);
int x_strcmp(char *si, char *di);
int main()
{
char buffer[256];
printf("Enter a string: ");
gets(buffer);
if(identf(buffer))
puts("合法的标识符");
else
puts("非法的标识符");
return 0;
}
/**************************************************************************/
int identf(char *str)
{
int flag=0;
char *p=str;
/*判定第一个字符是否符合*/
if((*str>='a' && *str<='z') || (*str>='A' && *str<='Z') || *str=='_')
{
flag=1;
str++;
}
/*从第二个字符开始遍历判定*/
while(*str && flag)
{
if((*str>='a' && *str<='z') || (*str>='A' && *str<='Z') || *str=='_'
|| (*str>='0' && *str<='9'))
{
flag=1;
str++;
}
else
flag=0;
}
/*判定是不是要害字*/
if(flag)
flag=isKey(p);
return flag;
}
/*****************************是不是要害字*******************************/
int isKey(char *str)
{
char *keyword[]={"auto", "break", "case", "char", "const", "continue", "default",
"do", "double", "else", "enum", "extern", "float", "for", "goto",
"if", "int", "long", "register", "return", "short", "signed",
"sizeof", "static", "struct", "switch", "typedef", "union", "unsigned",
"void", "volatile", "while"};
int i, flag=1;
for(i=0; i<32; i++)
{
if(x_strcmp(str, keyword[i])==0)
{
flag=0;
break;
}
}
return flag;
}
/*****************************字符串比较函数*******************************/
/* 为了练技术,自己写了一个strcmp() */
int x_strcmp(char *si, char *di)
{
while(*si && *di && *si==*di)
{
si++;
di++;
}
return *si-*di;
}
Word教程网 | Excel教程网 | Dreamweaver教程网 | Fireworks教程网 | PPT教程网 | FLASH教程网 | PS教程网 |
HTML教程网 | DIV CSS教程网 | FLASH AS教程网 | ACCESS教程网 | SQL SERVER教程网 | C语言教程网 | JAVASCRIPT教程网 |
ASP教程网 | ASP.NET教程网 | CorelDraw教程网 |