2017-07-30 14:52:59
0
I have a char array (let's say of size 4). I am converting it into a string using:
String str = String.valueOf(cf); // where cf is the char array of size 4.
This String can contain 01 or 11 or 001 or 011, etc.
Now, I need to calculate the number of digits in this string. But every single time I calculate the number of digits(preferably in Java), it shows 4 as the result(Maybe due to the size 4). How do I calculate the no. of digits according to input string?
Example: If 001 is the input, it should give o/p as 3 and so on.
Here's the coding part :
static long solve(int k, long n)
{
// System.out.println("Entered Solve function");
char[] c = new char[4];
long sum = 0;
char[] cf = {};
for(long i=2;i<=n;i++)
{
cf = fromDeci(c, k, i);
String str = String.valueOf(cf);
//System.out.println(snew);
sum = sum + str.length() ;
}
return sum;
}