#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define LEN 12
int main(void)
{
char name[LEN];
int i = 0, len = 0;
while(1)
{
len = 0;
printf("请输入name:");
gets(name);
if(strlen(name) >= LEN)
{
printf("你输入的字符串过长!\n");
continue;
}
/*检查输入的字符串是否合法*/
for(i = 0; i < strlen(name); ++i)
{
if(isdigit(name[i]) || isalpha(name[i]) || name[i] == '_')
{
len++;
}
else
{
printf("字符串中仅允许有数字、字母及下划线!\n");
break;
}
}
if(len == strlen(name)) /*如果通过检验的字符数与字符串长度相等,则输入的符合要求!*/
{
break; /*符合要求则退出循环,否则继续要求输入*/
}
}
printf("你刚才输入的是:%s\n", name);
system("pause");
return 0;
}