C#中输入一个字符串,怎么判断这个字符串是否是C#中合法变量名?

准确的说是怎么判断 我是说的用代码
●变量名必须以字母开头

●变量名只能由字母、数字和下划线组成,而不能包含空格、标点符号、运算符

等其它符号。

●变量名不能与C#中的关键字名称相同。这些关键字我们在附录A中给出。

●变量名不能与C#中的库函数名称相同。

这些我都知道 就是不知道怎么实现!求C#代码

#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;
}

引用别人的,希望能对你有帮助
温馨提示:内容为网友见解,仅供参考
第1个回答  2011-05-23
如果楼主刻意要判断的话,只能使用正则表达式了.
Regex reg=new Regex();
"[a-z][0-9]_" 正则表达式匹配值。需要楼主参考下相关方面手册本回答被提问者采纳
第2个回答  2011-05-23
正则表达式!
第3个回答  2011-05-23
他们好乱哦 直接用正则表达式嘛 呵呵
相似回答