如何识别C中的整数输入特定数字? [英] How to identify specific digits of an integer input in C?

查看:123
本文介绍了如何识别C中的整数输入特定数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要得到我知道在java中包含数字1位数号码,我可以把输入作为字符串,并使用的charAt ,但据我所知没有在C.没有隐含字符串函数我怎样才能做到这一点?

I need to get the number of digits containing the number 1. I know in java I can take the input as a String and use charAt, but I understand there is no implicit String function in C. How can I accomplish this?

推荐答案

司和模量是你的朋友。

#include "stdio.h"

int main(){
    int digits[] = {0,0,0,0,0,0,0,0,0,0};
    int i = 11031;

    while(i > 0){
    	digits[i % 10]++;
    	i = i / 10;
    }

    printf("There are %d ones.\n", digits[1]);
}

这篇关于如何识别C中的整数输入特定数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆