Skip to content
Advertisement

How to calculate a length of array with out using library

yesterday I appeared in interview,interviewer told me to write a code for calculating a length of array with out using a length property of Array class.

For examaple-

char[] array=new Scanner(System.in).nextLine().toCharArray();
// i have to write a code for calculating length of this array
//I can use any operator but use of library is restricted

All answer given here are using String library.

Answer

char[] array = new Scanner(System.in).nextLine().toCharArray();
int count = 0;
for (int i : array) {
    count++;
}
System.out.println(count);
Advertisement