Oxana Grey
Oxana Grey

Reputation: 387

how char array length works?

I've below code snippet

new char[(int)floor(log(25*(n+1)) / log(26))]

when n = 27, above returns 2

but why the length of above char array is two ?

Upvotes: 0

Views: 133

Answers (1)

Coder
Coder

Reputation: 1254

Let's go step by step:

25*(n+1) = 25 * 28 = 700

Assuming log is Math.log which is the natural log then we have:

log(25*(n+1)) = 6.551080335043404

Likewise:

log(26) = Math.log(26) = 3.258096538021482

Therefore floor(log(25*(n+1)) / log(26)) = floor(6.55/3.258) = floor(2.01) = 2

which yields 2 as expected.

In general, char[] c = new char[n]; will make a new character array c with size equal to n. This means that there will be n elements in your array from indices 0 to n-1.

Upvotes: 1

Related Questions