开发者

Array ValueCount

Suppose I have an array char buf[5]; that has a value stored in it, say "365". How can I count the number of digits present in that value? In this example, the answer is 3 开发者_开发知识库(3, 6, 5)?


Something like this should do it for you:

int length = strlen(buf);
int digits = 0;

for(int i=0; i< length; ++i) {
    if(isdigit(buf[i]))
        ++digits;   
}

printf( "Your word has %d digits in it\n", digits );


The isdigit() function can be used only when variable has integer value. The function isdigit() returns non-zero if its argument is a digit between '0' and '9'. Otherwise, zero is returned. You can understand it with help of following example.

#include <ctype.h>

int main()
{
    char c;
    c = buf[5];
    if ( isdigit(c) )
        printf( "You entered the digit %c\n", c );
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜