检查一个字符串是否只有C中的数字?

时间:2017-01-21 07:46:43

标签: c arrays char isalpha

我试图编写一个简单的代码来检查字符串中是否只有数字。到目前为止,它不起作用,任何帮助将不胜感激。

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

int main()
{
    char numbers[10];
    int i, correctNum = 0;

    scanf("%s", numbers);

    for(i = 0 ; i <= numbers ; ++i)
    {
        if(isalpha(numbers[i]))
        {
            correctNum = 1;
            break;
        }
    }

    if(correctNum == 1)
    {
        printf("That number has a char in it. FIX IT.\n");
    }
    else
    {
        printf("All numbers. Good.\n");
    }
    return 0;
}

5 个答案:

答案 0 :(得分:2)

添加其他答案,您还可以使用strtol来确定字符串是否包含所有数字。它基本上将字符串转换为整数,并省去任何非整数。您可以阅读man page以获取有关此功能的更多信息,以及您可以使用它进行的大量错误检查。

此外,您应该使用:

scanf("%9s", numbers);

而不是:

scanf("%s", numbers);

避免缓冲区溢出。

以下是一些示例代码:

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

#define MAXNUM 10
#define BASE 10

int main(void) {
    char numbers[MAXNUM];
    char *endptr;
    int number;

    printf("Enter string: ");
    scanf("%9s", numbers);

    number = strtol(numbers, &endptr, BASE);

    if (*endptr != '\0' || endptr == numbers) {
        printf("'%s' contains non-numbers\n", numbers);
    } else {
        printf("'%s' gives %d, which has all numbers\n", numbers, number);
    }

    return 0;
}

示例输入1:

Enter string: 1234

输出:

'1234' gives 1234, which has all numbers

示例输入2:

Enter string: 1234hello

输出:

'1234hello' contains non-numbers

答案 1 :(得分:0)

PR!A1:IF1000

运行从for(i = 0 ; i <= numbers ; ++i) //how is this supposed to work. 0的循环,小于输入的1

length

答案 2 :(得分:0)

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

int main()
{
    char numbers[10];
    int i, correctNum = 0;

    scanf("%s", numbers);

    for(i = 0 ; i < 10 ; i++)
    {
        if(numbers[i]<48||numbers[i]>57)
        {
            correctNum = 1;
            break;
        }
    }

    if(correctNum == 1)
    {
        printf("That number has a char in it. FIX IT.\n");
    }
    else
    {
        printf("All numbers. Good.\n");
    }
    return 0;
}

答案 3 :(得分:0)

您可以考虑使用strspn

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

int main(int argc, char* argv[]) {
    int i;
    for (i=1; i < argc; i++) {
        printf("%s %s\n",
            strlen(argv[i]) == strspn(argv[i], "0123456789") ? "digits" : "mixed",
            argv[i]
        );
    }
}

演示了:

$ ./try foo 123 ba23a 123.4
mixed foo
digits 123
mixed ba23a
mixed 123.4

strspn返回第二个参数中出现的第一个参数的初始字符数。超级简单的例子:

strspn("abba", "a");  // == 1
strspn("abba", "b");  // == 0
strspn("abba", "ab"); // == 2

答案 4 :(得分:-1)

for循环中有错误 - for(i = 0; i&lt; = numbers; ++ i)

数字是指针,禁止与整数进行比较。 正确的代码 -

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

int main()
{
   char numbers[10];
   int i, correctNum = 0;

    scanf("%s", numbers);

    for(i = 0 ; i < strlen(numbers) ; ++i)
    {
       if(!(numbers[i]>='0' && numbers[i]<='9'))
       {
           correctNum = 1;
            break;
       }
    }

    if(correctNum == 1)
    {
         printf("That number has a char in it. FIX IT.\n");
     }
   else
    {
        printf("All numbers. Good.\n");
    }
    return 0;
 }