如何检查输入是否是C中的字母?

时间:2016-03-19 23:20:40

标签: c

您好这部分代码。它检查输入的值是否在基数2中。 我输入整数值时工作。但是我希望得到代码来检查字母字符输入。我怎样才能做到这一点?

#include<stdio.h>
#include<stdlib.h>
#include<math.h>

int main()
{
    int DataForBase1,DataForBase1A,CheckForBase1;

    printf("For disk1 enter data in base 2: ");
    scanf("%d",&DataForBase1);

    DataForBase1A=DataForBase1;

    while(DataForBase1!=0)
    {
        CheckForBase1=DataForBase1%10;
        if( (CheckForBase1!=0) && (CheckForBase1!=1) ) 
        {
            printf("ERROR: This is invalid input for base 2\n");
            printf("For disk1 enter data in base 2: ");
            scanf("%d",&DataForBase1);
        }
        else
            DataForBase1=DataForBase1/10;       
    }

    system("pause");
    return 0;
}

3 个答案:

答案 0 :(得分:1)

%c %d之后使用scanf() 例如:

int DataForBase1,DataForBase1A,CheckForBase1;
char ch;

printf("For disk1 enter data in base 2: ");
scanf("%d%c",&DataForBase2, &ch);

这样,

  • 如果您输入的数字如11d,63f 。,%c会在数字后输入额外字符
  • 输入纯数值,如11,63 %c将保留\n个字符。因此,您的程序将正常工作。

PS。:根据您发布相同代码的另一个“刚删除”帖子输入假设。

答案 1 :(得分:1)

考虑一下 http://www.tutorialspoint.com/c_standard_library/ctype_h.htm

#include<stdio.h>
#include<stdlib.h>
#include<math.h>

#include <string.h>
#include <ctype.h>

#define MAX   80   /* max characters to read in */


int Parse_String ( char str[], int *DataForBase1 )
 {
       /* do all checking on user input */
       /* use functions from ctype.h  and  string.h */
       /* ctype.h will have functions to allow checking if number or character */

       int i, len, value;
       int result = 1;

       *DataForBase1 = -1;

       len = strlen( str );
       for ( i = 0; i < len; i++ )
       {
           if ( ! isalnum( str[i] )
           {
              result = 0;
              break;
           }
        }

        /* write another for loop here checking every character is either 0 or 1 */
        /* and if any is not then set result = 0 and handle accordingly */

        i = sscanf( str, "%d", &value );
        if ( i != 1 )
        {
           *DataForBase1 = -1;
            result = 0;
        }
        else
        {
            *DataForBase1 = value;
        }

       return result;
 }

int main()
{
    char str[MAX];
    int result;
    int DataForBase1,DataForBase1A,CheckForBase1;

    printf("For disk1 enter data in base 2: ");
    fgets( str, MAX, stdin );

    result = Parse_String( str, &DataForBase1 );

    if ( result == 1 )
       DataForBase1A=DataForBase1;
    else
    {
       /* handle error condition here */
    }

    while(DataForBase1!=0)
    {
        CheckForBase1=DataForBase1%10;
        if( (CheckForBase1!=0) && (CheckForBase1!=1) ) 
        {
            printf("ERROR: This is invalid input for base 2\n");
            printf("For disk1 enter data in base 2: ");
            scanf("%d",&DataForBase1);
        }
        else
            DataForBase1=DataForBase1/10;       
    }

    system("pause");
    return 0;
}

答案 2 :(得分:0)

您可以确定当前输入位置的字符不代表您可以执行此操作的数字:

int count = scanf("%d", &DataForBase1);
if (count == 0) {
    // Nothing was read
    ...
}

如果检测到scanf已返回零,则冷读并忽略字符串,打印错误消息,然后循环返回以再次读取数值。