[英]Check whether the input is digit or not in C programming
I am currently reading this book: The C Programming Language - By Kernighan and Ritchie (second Edition) and one of the examples I am having trouble understanding how to check whether the input is digit or not. 我目前正在阅读这本书: C编程语言-Kernighan和Ritchie(第二版),以及其中的一个示例,使我难以理解如何检查输入是否为数字。 The example is on Page 22, explaining under the array chapter. 该示例在第22页上,在数组一章下进行了说明。
Below is the example. 以下是示例。
#include <stdio.h>
/* count digits, white space, others */
main()
{
int c, i, nwhite, nother;
int ndigit[10];
nwhite = nother = 0;
for (i = 0; i < 10; ++i)
{
ndigit[i] = 0;
}
while ((c = getchar()) != EOF)
{
if (c >= '0' && c <= '9')
{
++ndigit[c-'0'];
}
else if (c == ' ' || c == '\n' || c == '\t')
{
++nwhite;
}
else
{
++nother;
}
printf("digits =");
for (i = 0; i < 10; ++i)
{
printf(" %d", ndigit[i]);
}
printf(", white space = %d, other = %d\n",nwhite, nother);
}
For this example, what confused me is that the author mentioned that the line ++ndigit[c-'0']
checks whether the input character in c is a digit or not. 对于此示例,令我感到困惑的是,作者提到++ndigit[c-'0']
检查c中的输入字符是否为数字。 However, I believe that only the if statement ( if (c>= '0' && c<= '9') )
is necessary, and it will check if c is digit or not. 但是,我相信只有if语句( if (c>= '0' && c<= '9') )
是必需的,它将检查c是否为数字。 Plus, I do not understand why [c-'0']
will check the input(c) is digit or not while the input variable (c) is subtracted from the string-casting ('0'). 另外,我不理解为什么[c-'0']
会在从字符串转换('0')中减去输入变量(c)时检查输入(c)是否为数字。
Any suggestions/explanations would be really appreciated. 任何建议/解释将不胜感激。
Thanks in advance :) 提前致谢 :)
The if
statement checks whether the character is a digit, and the ++ndigit[c-'0']
statement updates the count for that digit. if
语句检查字符是否为数字,而++ndigit[c-'0']
语句更新该数字的计数。 When c
is a character between '0'
and '9'
, then c-'0'
is a number between 0
and 9
. 当c
是介于'0'
和'9'
之间的字符时,则c-'0'
是介于0
和9
之间的数字。 To put it another way, the ASCII value for '0'
is 48 decimal, '1'
is 49, '2'
is 50, etc. So c-'0'
is the same as c-48
, and converts 48,49,50,...
to 0,1,2...
换句话说,ASCII值'0'
是十进制的48, '1'
是49, '2'
是50, c-'0'
。因此c-'0'
与c-48
相同,并转换48,49,50,...
至0,1,2...
One way to improve your understanding is to add a printf
to the code, eg replace 增进理解的一种方法是在代码中添加一个printf
,例如replace
if (c >= '0' && c <= '9')
++ndigit[c-'0'];
with 同
if (c >= '0' && c <= '9')
{
++ndigit[c-'0'];
printf( "is digit '%c' ASCII=%d array_index=%d\n", c, c, c-'0' );
}
I would try to explain with an example 我会尝试用一个例子来解释
suppose the input is abc12323 假设输入是abc12323
So the frequency of 1=1 所以1 = 1的频率
frequency of 2=2 频率2 = 2
frequency of 3=2 频率3 = 2
if (c >= '0' && c <= '9') //checks whether c is a digit
++ndigit[c-'0'];
Now if you do printf("%d",c) then you will get the ascii value of the 现在,如果您执行printf(“%d”,c),则将获得
character 字符
for c='0' the ascii value will be 48,c='1' ascii value will be 49 and it goes 对于c ='0',ascii值为48,c ='1'ascii值为49,
till 57 for c='9'. 直到57代表c ='9'。
In your program you are keeping a frequency of the digits in the input so you need to update the index of the digit in the array every time you get it 在您的程序中,您要保持输入中数字的频率,因此每次获取时都需要更新数组中数字的索引
if you do ndigit[c]++ then it will update ndigit[48] for c='0',ndigit[49] for c='1' 如果您执行ndigit [c] ++,则它将更新c ='0'的ndigit [48],更新c ='1'的ndigit [49]
So either you can do ndigit[c-'0']++ as ascii value of '0'=48 in decimal 因此,您都可以将ndigit [c-'0'] ++的ascii值设为'0'= 48(十进制)
or you can simply do ndigit[c-48]++ so for c='0' ndigit[0] is updated,c=1' 或者您可以简单地执行ndigit [c-48] ++,这样对于c ='0'ndigit [0]被更新,c = 1'
ndigit[1] is updated ndigit [1]已更新
you can check the re factored code here http://ideone.com/nWZxL1 您可以在此处查看重构的代码http://ideone.com/nWZxL1
Hope it helps you,Happy Coding 希望对您有帮助,快乐编码
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.