#include <stdio.h>
main()
{
char chset[256],s[512];
int i;
for(i=0;i<256;i++)chset[i]=0;
for(i='0';i<='9';i++)chset[i]=1;
for(i='a';i<='z';i++)chset[i]=chset[i-32]=2;
chset['_']=3;
for(;;)
{
printf("Please provide a variable name:") ;
scanf("%s",s) ;
if('0'==*(short*)s) break ;
if( chset[s[i=0]] > 1 ) while(chset[s[++i]]) ;
printf((s[i])?"INVALID\n":"VALID\n") ;
}
}
-------------
问题补充:
不能使用阵列
输入“0”时退出 改为不输入变量名直接按ENTER时退出
-------------
#include <stdio.h>
int isfirst(int x){
return (x!='_') &&
(x<'A' || x>'Z') &&
(x<'a' || x>'z')? 0:1;
}
int iselse(int x){
return ( (x!='_') &&
(x<'0' || x>'9') &&
(x<'A' || x>'Z') &&
(x<'a' || x>'z') )? 0:1;
}
int main(void) {
char key; /* input character from user */
int match; /* keep track of characters matched */
int count; /* number of substring matches */
while (1) {
count = 0;
match = 0;
printf("Please provide a variable name:");
while ((key = getchar()) != '\n') {
count++;
switch (match) {
case 0: /* starting - not matches yet */
if (isfirst(key)) match = 1;
else match = -1;
break;
default:
if (match > 0)
if (iselse(key)) match++;
else match = - match - 1;
break;
}
}
if (count == 0) return 0;
if (match < 0) match = - match - 1;
printf("%d matches of %d\n", match, count);
}
return 0;
}
温馨提示:答案为网友推荐,仅供参考