code_junkie

How to identify specific digits of an integer input in C?

Nov 10th, 2011
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.23 KB | None | 0 0
  1. #include "stdio.h"
  2.  
  3. int main(){
  4. int digits[] = {0,0,0,0,0,0,0,0,0,0};
  5. int i = 11031;
  6.  
  7. while(i > 0){
  8. digits[i % 10]++;
  9. i = i / 10;
  10. }
  11.  
  12. printf("There are %d ones.n", digits[1]);
  13. }
  14.  
  15. int val; //Input
  16. ...
  17. int ones = 0;
  18. while(val != 0) {
  19. ones += ((val % 10) == 1) ? 1 : 0;
  20. val /= 10;
  21. }
  22.  
  23. while(*str != '') {
  24. if(*str++ == '1') {
  25. ones++;
  26. }
  27. }
  28.  
  29. "java".charAt(i) == "c the language"[i];
  30.  
  31. int val=11031;
  32. int count=0;
  33. int i=0;
  34. char buf[100];
  35. sprint(buf, "%d", val);
  36. for(i=0; (i < sizeof(buf)) && (buf[i]); i++) {
  37. if(buf[i] == '1')
  38. count++;
  39. }
  40.  
  41. int digit = 0;
  42. int value = 11031;
  43.  
  44. while(value > 0)
  45. {
  46. digit = value % 10;
  47. /* Do something with digit... */
  48. value = value / 10;
  49. }
  50.  
  51. int count_digit(int nr, int digit) {
  52. int count=0;
  53. while(nr>0) {
  54. if(nr%10==digit)
  55. count++;
  56. nr=nr/10;
  57. }
  58. return count;
  59. }
  60.  
  61. int count_digits(int digit, const char *in)
  62. {
  63. int ii, count = 0;
  64. if (digit < 0 || digit > 9) return -0;
  65. digit = digit + (int)'0'; /* make the digit the correct ascii value */
  66.  
  67. for (ii = 0; in[ii] != ''; ++ii)
  68. {
  69. if (in[ii] == (char)digit) count++;
  70. }
  71.  
  72. return count;
  73. }
  74.  
  75. int count_chars(char ch, const char *in)
  76. {
  77. int ii, count = 0;
  78. for (ii = 0; in != NULL && in[ii] != ''; ++ii)
  79. {
  80. if (in[ii] == ch) count++;
  81. }
  82.  
  83. return count;
  84. }
  85.  
  86. int countOnes(int input)
  87. {
  88. int result = 0;
  89. while(input) {
  90. result += ((input%10)==1);
  91. result /= 10;
  92. }
  93. return result;
  94. }
  95.  
  96. int countOnes(char *input)
  97. {
  98. int result = 0;
  99. while(input && *input) {
  100. result += (*input++ == '1');
  101. }
  102. return result;
  103. }
  104.  
  105. int countDigit(int Number, int Digit)
  106. {
  107. int counter = 0;
  108.  
  109. do
  110. {
  111. if( (Number%10) == Digit)
  112. {
  113. counter++;
  114. }
  115. }while(Digit>0)
  116.  
  117. return counter;
  118. }
  119.  
  120. #include <stdio.h>
  121.  
  122. main() {
  123. char buf[100];
  124. char *p = buf;
  125. int n = 0;
  126. scanf("%s", buf);
  127. while (*p) {
  128. if (*p == '1') {
  129. n++;
  130. }
  131. p++;
  132. }
  133. printf ("'%s' contains %i onesn", buf, n);
  134. }
  135.  
  136. int count_digits(int n, int d) {
  137. int count = 0;
  138. while(n*10/=10) if (n%10==d) count++
  139. return count;
  140. }
Add Comment
Please, Sign In to add comment