0
char name[50];
int valid;

do{
      valid=1;
      printf("Enter name for Salesman: ");
      fgets(name,50,stdin);
      if(!isalpha(*name)) //it will only scan for the first character of string
      {
          printf("Only alphabet is allowed.\n");
          valid=0;
      }
  }while(valid==0);

我希望整个字符串没有任何数字输入。

4

2 回答 2

1

您将不得不处理整个字符串:

int i = 0;
for (i = 0; i < strlen(name) && i < 50; i++) {
  if (!isalpha(name[i])) {
    valid = 0;
    printf("Please only use alphabetic characters!\n");
    break;
  }
}
于 2012-11-01T11:37:20.107 回答
0

C语言不是像java那样支持正则表达式吗?

所以我们可以像这样在java中做到这一点:

String test= "Any String even if 1 digit is in between";

Pattern p = Pattern.compile("[0-9]+");

Matcher m = p.matcher(test);

if (m.find())
{               
    System.out.println("Numeric is not allowed");
}
于 2012-11-01T12:45:33.367 回答