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

[{'answer_id': 66122, 'author': 'Aaron Maenpaa', 'author_id': 2603, 'author_profile': 'https://Stackoverflow.com/users/2603', 'pm_score': 3, 'selected': False, 'text': '<p>Division and modulus are your friends.</p>\n\n<pre><code>#include "stdio.h"\n\nint main(){\n    int digits[] = {0,0,0,0,0,0,0,0,0,0};\n    int i = 11031;\n\n    while(i &gt; 0){\n        digits[i % 10]++;\n        i = i / 10;\n    }\n\n    printf("There are %d ones.\\n", digits[1]);\n}\n</code></pre>\n'}, {'answer_id': 66138, 'author': 'John Millikin', 'author_id': 3560, 'author_profile': 'https://Stackoverflow.com/users/3560', 'pm_score': 1, 'selected': False, 'text': "<p>Homework?</p>\n\n<p>You'd read it into a <code>char*</code> using the <code>fread()</code> function, and then store how many bytes were read in a separate variable. Then use a <code>for</code> loop to iterate through the buffer and count how many of each byte are present.</p>\n"}, {'answer_id': 66151, 'author': 'Patrick', 'author_id': 429, 'author_profile': 'https://Stackoverflow.com/users/429', 'pm_score': 1, 'selected': False, 'text': '<p>If you have just the number, then you can do this:</p>\n\n<pre><code> int val; //Input\n ...\n int ones = 0;\n while(val != 0) {\n   ones += ((val % 10) == 1) ? 1 : 0;\n   val /= 10;\n }\n</code></pre>\n\n<p>If you have a string (char*), the you\'d do something like this:</p>\n\n<pre><code>while(*str != \'\\0\') {\n  if(*str++ == \'1\') {\n    ones++;\n  }\n}\n</code></pre>\n\n<p>It\'s also worth noting that c does have a charAt function, in a way:</p>\n\n<pre><code>"java".charAt(i) == "c the language"[i];\n</code></pre>\n\n<p>By indexing into the char*, you can get the value you want, but you need to be careful, because there is no indexOutOfBounds exception. The program will crash if you go over the end of a string, or worse it may continue running, but have a messed up internal state.</p>\n'}, {'answer_id': 66154, 'author': 'kfh', 'author_id': 6597, 'author_profile': 'https://Stackoverflow.com/users/6597', 'pm_score': 0, 'selected': False, 'text': '<p>Something along the lines of:</p>\n\n<pre><code>int val=11031;\nint count=0;\nint i=0;\nchar buf[100];\nsprint(buf, "%d", val);\nfor(i=0; (i &lt; sizeof(buf)) &amp;&amp; (buf[i]); i++) {\n  if(buf[i] == \'1\')\n    count++;\n}\n</code></pre>\n'}, {'answer_id': 66171, 'author': 'Jarrett Meyer', 'author_id': 5834, 'author_profile': 'https://Stackoverflow.com/users/5834', 'pm_score': 1, 'selected': False, 'text': '<p>Try something like...</p>\n\n<pre><code>int digit = 0;\nint value = 11031;\n\nwhile(value &gt; 0)\n{\n    digit = value % 10;\n    /* Do something with digit... */\n    value = value / 10;\n}\n</code></pre>\n'}, {'answer_id': 66176, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': '<pre><code>int count_digit(int nr, int digit) {\n    int count=0;\n    while(nr&gt;0) {\n      if(nr%10==digit)\n        count++;\n      nr=nr/10;\n    }\n    return count;\n}\n</code></pre>\n'}, {'answer_id': 66214, 'author': 'Robᵩ', 'author_id': 8747, 'author_profile': 'https://Stackoverflow.com/users/8747', 'pm_score': 0, 'selected': False, 'text': '<p>This sounds like a homework problem to me. Oh well, it\'s your life.</p>\n\n<p>You failed to specify the type of the variable that contains the "input integer". If the input integer is an integral type (say, an "int") try this:</p>\n\n<pre><code>int countOnes(int input)\n{\n    int result = 0;\n    while(input) {\n        result += ((input%10)==1);\n        result /= 10;\n    }\n    return result;\n}\n</code></pre>\n\n<p>If the "input integer" is in a string, try this:</p>\n\n<pre><code>int countOnes(char *input)\n{\n    int result = 0;\n    while(input &amp;&amp; *input) {\n        result += (*input++ == \'1\');\n    }\n    return result;\n}\n</code></pre>\n\n<p>Hope this helps. Next time, do your own homework. And get off of my lawn! Kids, these days, ...</p>\n'}, {'answer_id': 66256, 'author': 'Robert Deml', 'author_id': 9516, 'author_profile': 'https://Stackoverflow.com/users/9516', 'pm_score': 0, 'selected': False, 'text': '<pre><code>int countDigit(int Number, int Digit)\n{\n   int counter = 0;\n\n   do\n   {\n      if( (Number%10) == Digit)\n      {\n         counter++;\n      }\n   }while(Digit&gt;0)\n\n   return counter;\n}\n</code></pre>\n'}, {'answer_id': 66347, 'author': 'Mr Shark', 'author_id': 6093, 'author_profile': 'https://Stackoverflow.com/users/6093', 'pm_score': 0, 'selected': False, 'text': '<p>Something along the lines of this:</p>\n\n<pre><code>#include &lt;stdio.h&gt;\n\nmain() {\n        char buf[100];\n        char *p = buf;\n        int n = 0;\n        scanf("%s", buf);\n        while (*p) {\n                if (*p == \'1\') {\n                        n++;\n                }\n                p++;\n        }\n        printf ("\'%s\' contains %i ones\\n", buf, n);\n}\n</code></pre>\n'}, {'answer_id': 67196, 'author': 'hoyhoy', 'author_id': 3499, 'author_profile': 'https://Stackoverflow.com/users/3499', 'pm_score': 0, 'selected': False, 'text': '<p>This will do it.  :-)</p>\n\n<pre><code>int count_digits(int n, int d) {\n  int count = 0;\n  while(n*10/=10) if (n%10==d) count++\n  return count;\n}\n</code></pre>\n'}, {'answer_id': 67308, 'author': 'melfar', 'author_id': 9804, 'author_profile': 'https://Stackoverflow.com/users/9804', 'pm_score': 0, 'selected': False, 'text': "<p>For all those who refer to the question as the homework question: I have to say, most of you provided a homework answer.  </p>\n\n<p>You don't do division/modulus to get the digits in production code, firstly because it's suboptimal (your CPU is designed for binary arithmetics not decimal) and secondly because it's unintuitive.  Even if it's not originally a string, it's more optimal to convert it to one and then count the characters (std::count is the way to go in C++).</p>\n"}, {'answer_id': 69696, 'author': 'Ren', 'author_id': 11188, 'author_profile': 'https://Stackoverflow.com/users/11188', 'pm_score': 1, 'selected': False, 'text': '<p>I see this as a basic understanding problem, which inevitably everyone goes through switching from one language to the next.  </p>\n\n<p>A good reference to go through to understand how string\'s work in C when you\'ve started familiarity with java is look at how string.h works.  Where as in java string\'s are an Object and built in, strings in C are just integer arrays.</p>\n\n<p>There are a lot of tutorials out there, one that helped me when I was starting earlier in the year was <a href="http://www.physics.drexel.edu/students/courses/Comp_Phys/General/C_basics/" rel="nofollow noreferrer">http://www.physics.drexel.edu/students/courses/Comp_Phys/General/C_basics/</a> look at the string section.</p>\n\n<p>Sometimes asking a question speeds up learning a lot faster than pouring through the text book for hours on end.</p>\n'}]
['https://Stackoverflow.com/questions/66107', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/9628/']