通过读取字符串来检测数据类型

时间:2021-08-03 16:55:33

So I've wondered, what would be the simplest way to check for user input (stdin). I came to conclusion, ideally it would be to scanf an user input and print out the results.

因此我想知道,检查用户输入(stdin)的最简单方法是什么。我得出结论,理想的情况是扫描用户输入并打印结果。

Though now I'm somewhat confused, about how should I do this. Here's what I was thinking:

虽然现在我有点困惑,我该怎么做。我是这么想的:

#include <stdio.h>
int main(){

char input[30];    

 printf("Please enter text\n");
 scanf("%s", &input);

 ...

So here's the part I can't really wrap my head around of. So what I'd like to do, is run through the entire word (input), character by character.

这是我不能完全理解的部分。所以我想要做的是,把整个单词(输入),字符按字符运行。

Basically, if the string consits of only numbers (0-9), I'd like the input to be identified as a number. Otherwise, detect it as a string.

基本上,如果字符串只包含数字(0-9),我希望输入被标识为数字。否则,将其检测为字符串。

I've done a fair bit of research (although keep in mind I'm an absolute beginner), there is a way with strcmp() function, but I'd prefer avoiding other libraries, such as string.h altogether and just do it the simple way, as I've tried to explain.

我已经做了一些研究(尽管记住我是一个绝对的初学者),有一种使用strcmp()函数的方法,但是我更喜欢避免使用其他库,比如string。就像我解释的那样,用简单的方法。

1 个解决方案

#1


3  

Just do it.

想做就做。

#include <stdio.h>

int checkIfNumber(const char *word) {
    /* check until the string ends */
    while (*word != '\0') {
        /* return 0 if the character isn't a number */
        if (*word < '0' || '9' < *word) return 0;
        /* proceed to the next character */
        word++;
    }
    /* no characters other than numbers found */
    return 1;
}

int main(void){

    char input[30];

    printf("Please enter text\n");
    scanf("%29s", input);

    if(checkIfNumber(input)) {
        printf("%s is number\n", input);
    } else {
        printf("%s is string\n", input);
    }

    return 0;
}

Note that character code for numbers are continuous in C, so this range-based method is useful for checking if a character is a number (decimal digit). This method may not work for alphabets, especially on systems on which non-ASCII character code is used.

注意,数字的字符代码在C中是连续的,因此这种基于范围的方法对于检查字符是否为数字(十进制数字)很有用。这种方法可能不适用于字母表,尤其是在使用非ascii字符代码的系统上。

N1256 5.2.1 Character sets

N1256 5.2.1字符集

In both the source and execution basic character sets, the value of each character after 0 in the above list of decimal digits shall be one greater than the value of the previous.

在源和执行基本字符集中,以上十进制数列表中0后的每个字符的值都要大于前一个字符的值。

#1


3  

Just do it.

想做就做。

#include <stdio.h>

int checkIfNumber(const char *word) {
    /* check until the string ends */
    while (*word != '\0') {
        /* return 0 if the character isn't a number */
        if (*word < '0' || '9' < *word) return 0;
        /* proceed to the next character */
        word++;
    }
    /* no characters other than numbers found */
    return 1;
}

int main(void){

    char input[30];

    printf("Please enter text\n");
    scanf("%29s", input);

    if(checkIfNumber(input)) {
        printf("%s is number\n", input);
    } else {
        printf("%s is string\n", input);
    }

    return 0;
}

Note that character code for numbers are continuous in C, so this range-based method is useful for checking if a character is a number (decimal digit). This method may not work for alphabets, especially on systems on which non-ASCII character code is used.

注意,数字的字符代码在C中是连续的,因此这种基于范围的方法对于检查字符是否为数字(十进制数字)很有用。这种方法可能不适用于字母表,尤其是在使用非ascii字符代码的系统上。

N1256 5.2.1 Character sets

N1256 5.2.1字符集

In both the source and execution basic character sets, the value of each character after 0 in the above list of decimal digits shall be one greater than the value of the previous.

在源和执行基本字符集中,以上十进制数列表中0后的每个字符的值都要大于前一个字符的值。