digits

Given a string consisting of alphabets and digits, find the frequency of each digit in the given string

狂风中的少年 提交于 2019-12-02 22:54:48
问题 Please tell me whats wrong in code the output is 00000000. I know there is some mistake but cant find it. #include <stdio.h> #include <string.h> int main() { int c=0; char s[100]; fgets(s, 100, stdin); printf("%s", s); for(int i=0;i<=9;i++) { for(int j=0;j<strlen(s);j++) { if(s[j]==i){ c++; } } printf("%d", c); } return 0; } 回答1: Firstly, you are comparing a character with a int. Have a look at Char to int conversion in C for a solution. Then, I would remember you that "0" is index 48 in

Delete digits in Python (Regex)

怎甘沉沦 提交于 2019-12-02 19:19:38
I am trying to delete all digits from a string. However, the next code deletes as well digits contained in any word. Obviously, I don't want that. I have been trying many regular expressions with no success. Thanks! s = "This must not be deleted, but the number at the end yes 134411" s = re.sub("\d+", "", s) print s Result: This must not b deleted, but the number at the end yes Add a space before the \d+. >>> s = "This must not b3 delet3d, but the number at the end yes 134411" >>> s = re.sub(" \d+", " ", s) >>> s 'This must not b3 delet3d, but the number at the end yes ' Edit: After looking at

How do I separate an integer into separate digits in an array in JavaScript?

戏子无情 提交于 2019-12-02 14:17:11
This is my code so far: var n = 123456789; var d = n.toString().length; var digits = []; var squaredDigits = []; for (i = d; i >= 1; i--) { var j = k / 10; var r = (n % k / j) - 0.5; var k = Math.pow(10, i); var result = r.toFixed(); digits.push(result); } console.log(digits); But when I run my code I get this: [9, 1, 2, 3, 4, 5, 6, 7, 8] If anyone can see the problem or find a better solution I would very much appreciate it! Why not just do this? var n = 123456789; var digits = (""+n).split(""); (123456789).toString(10).split("") ^^ this will return an array of strings (123456789).toString(10

Convert decimal number to vector

▼魔方 西西 提交于 2019-12-02 12:21:11
In matlab I want to convert this: 12345.6788993442355456789 into a vector [1 2 3 4 5 6 7 8 8 9 9 3 4 4 2 3 5 5 4 5 6 7 8 9] I have found several solutions to convert a integer into a vector using commands like scanf, num2str,... but those don't fit with non-integers, and some solutions have problems with numbers with a large number of decimal places... That kind of conversion is needed because I want to see and use all of the digits of the number. Andrew Janke What input source are you getting those numbers from? And are all those digits significant? Your example numbers are already beyond the

Calculate number of decimal places for a float value WITHOUT libraries?

六眼飞鱼酱① 提交于 2019-12-02 11:45:46
问题 I need to calculate the number of decimal places for a float value, e.g. 1234. 567 -> 3 2. 1233 -> 4 4. 2432 -> 4 My initial idea was: number = 1234.567; ... while (number - (int)number > 0.0) { // Count decimal places ... number *= 10; } However, this causes problems with float precision in the while-condition. The only safe workaround would be a conversion from float to string and then do string-based counting of decimal places. The problem is: I must NOT use any libraries, neither third

Get a number from an array of digits

这一生的挚爱 提交于 2019-12-02 09:14:09
问题 To split a number into digits in a given base, Julia has the digits() function: julia> digits(36, base = 4) 3-element Array{Int64,1}: 0 1 2 What's the reverse operation? If you have an array of digits and the base, is there a built-in way to convert that to a number? I could print the array to a string and use parse(), but that sounds inefficient, and also wouldn't work for bases > 10. 回答1: The answer seems to be written directly within the documentation of digits : help?> digits search:

Count number of occurrences of a digit within a string

别来无恙 提交于 2019-12-02 09:08:22
问题 So I'm trying to count the number of occurrences of each digit within an array. My code I've got so far looks like the following: #include <stdio.h> #include <string.h> int main() { int numbers [10]= {1, 4, 5, 5, 5, 6, 6, 3, 2, 1}; int count = 0; for(int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { if (numbers[i] == numbers[j]) { count++; } } printf("Number %d has occured %d many times \n", numbers[i], count); count = 0; } } Only the output I get is the following: Number: 1 Occurence:

Count number of occurrences of a digit within a string

回眸只為那壹抹淺笑 提交于 2019-12-02 05:45:58
So I'm trying to count the number of occurrences of each digit within an array. My code I've got so far looks like the following: #include <stdio.h> #include <string.h> int main() { int numbers [10]= {1, 4, 5, 5, 5, 6, 6, 3, 2, 1}; int count = 0; for(int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { if (numbers[i] == numbers[j]) { count++; } } printf("Number %d has occured %d many times \n", numbers[i], count); count = 0; } } Only the output I get is the following: Number: 1 Occurence: 2 Number: 4 Occurence: 1 Number: 5 Occurence: 3 Number: 5 Occurence: 3 Number: 5 Occurence: 3 Number:

Show more digits in PHP

左心房为你撑大大i 提交于 2019-12-01 22:32:45
问题 Let's say I have: echo 1/3; And it print out only 0.33333333333333, can I get more digits? 回答1: Can use bcdiv echo bcdiv(1, 3, 20); The third argument is used to set the number of digits after the decimal place in the result. You can also set the global default scale for all functions by using bcscale(). 回答2: Edit the precision configuration variable either in your php.ini or some other configuration location or use ini_set(). ini_set('precision', 22); echo 1/3; // 0.3333333333333333148296

Show more digits in PHP

安稳与你 提交于 2019-12-01 20:46:30
Let's say I have: echo 1/3; And it print out only 0.33333333333333, can I get more digits? Gordon Can use bcdiv echo bcdiv(1, 3, 20); The third argument is used to set the number of digits after the decimal place in the result. You can also set the global default scale for all functions by using bcscale(). Edit the precision configuration variable either in your php.ini or some other configuration location or use ini_set() . ini_set('precision', 22); echo 1/3; // 0.3333333333333333148296 Even though I highly doubt that you really need that kind of precision ;-) EDIT As Gordon said: you'll hit