1

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

#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;
}
4

5 回答 5

4

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

此外,您应该使用:

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
于 2017-01-21T10:21:12.337 回答
0
for(i = 0 ; i <= numbers ; ++i) //how is this supposed to work.

运行循环 from01小于length输入的 。

for(i = 0 ; i < strlen(numbers) ; ++i)
于 2017-01-21T07:53:04.093 回答
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;
}
于 2017-01-21T08:01:36.003 回答
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
于 2017-10-05T19:27:30.187 回答
-1

您在 for 循环中有错误 - for(i = 0 ; i <= numbers ; ++i)

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

#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;
 }
于 2017-01-21T07:52:34.737 回答