【问题标题】:how to add validation mr or mrs before name如何在姓名前添加验证 mr 或 mrs
【发布时间】:2019-08-07 10:57:42
【问题描述】:

我正在通过 do while 语句使用 Mr 或 ms 或 Mrs 在姓名前输入姓名进行验证。我应该在while部分填写什么?。

是使用 strcmp 还是别的什么?

编码示例

do{
    printf("Input Customer's Name [must have Mr. or Ms. or Mrs");
    scanf("%[^\n]", &customerName);
}while(what should i fill here?);

【问题讨论】:

  • 在 C 规范中明确提到使用仅输入流(如 stdin)调用 fflush未定义行为。一些库将其添加为 non-portable 扩展。我建议您改为阅读整行,例如fgets 然后使用sscanf 解析字符串。并且记得检查sscanf 返回的内容。哦,还有一些吹毛求疵:do ... whilestatement 而不是函数。

标签: c algorithm fgets c-strings memcmp


【解决方案1】:

编写一个单独的函数来检查名称是否以列出的字符串开头。

例如

#include <stdio.h>
#include <string.h>

int start_with( const char *s, const char * a[] )
{
    int success = 0;

    s += strspn( s, " \t" );

    for ( ; *a != NULL && !success; ++a )
    {
        success = memcmp( s, *a, strlen( *a ) ) == 0;
    }

    return success;;
}

int main(void) 
{
    const char *title[] = { "Mr.", "Ms.", "Mrs.", NULL };
    enum { N = 100 };
    char name[N];

    do
    {
        printf( "Input Customer's Name [must have %s or %s or %s: ", title[0], title[1], title[2] );
        name[0] = '\0';
        fgets( name, N, stdin );
        name[strcspn( name, "\n" )] = '\0';
    } while ( !start_with( name, title ) );

    puts( name );

    return 0;
}

程序输出可能看起来像

Input Customer's Name [must have Mr. or Ms. or Mrs.: Bob
Input Customer's Name [must have Mr. or Ms. or Mrs.: Mr. Bob
Mr. Bob

【讨论】:

    【解决方案2】:

    您需要检查子字符串 'Mr.' 'Mrs.''Ms' 是否存在于用户提供的输入字符串中。 如果存在,您可以跳出循环,否则再次提示他/她。

    需要注意的是,这个问题中的子字符串位置必须是0(即在输入字符串的最开始处)

    您可以使用 strstr 函数,因为它不仅会检查子字符串,还会返回指向第一次出现的指针。

    例如

    #include <stdio.h>
    
    int main(void) {
    
        char customerName[50]; 
        char *titles[3] = {"Mr.","Mrs.","Ms."}; 
        char *titlePos; 
        int istitle = 0;
    
        do{
    
           printf("Input Customer's Name [must have Mr. or Ms. or Mrs \n");
           scanf("%[^\n]%*c",customerName);
    
           for(int i=0;i<3 && !istitle;i++){
           // checking if any of the title is present 
    
              titlePos=strstr(customerName,titles[i]);
              if(titlePos && (int)(titlePos-customerName)==0) {
                istitle=1; // title found 
                printf("CustomerName Consist the Title of %s \n",titles[i]);
    
            }
        }
    }while(!istitle);
    
      // ... Rest of Your Code
    
        return 0;
    }
    
    

    【讨论】:

    • 使用strcmp?例如:while(strcmp(customerName, "Mr ")!= 0 || strcmp(customerName, "Ms ")!= 0 || strcmp(customerName, "Mrs ")!= 0);
    • 我建议您使用 strstr,因为您需要检查“先生”是否存在'太太。'等在给定输入名称的第一个位置
    【解决方案3】:

    如果您坚持只在您的编码示例中更改what should i fill here?,最重要的是要输入getchar(),以消耗输入中剩余的换行符,否则我们会得到一个无限循环。但通常情况下,可以在scanf() 中使用格式字符串"%[^\n]\n""%[^\n] "(等效)来执行此操作。

    除此之外,表达式

    strncmp("Mr.", customerName, 3) &&
    strncmp("Ms.", customerName, 3) &&
    strncmp("Mrs", customerName, 3)
    

    会的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-18
      相关资源
      最近更新 更多