ProgrammingHard
for (i = 0; i < strlen(s); i++) process(s[i]); with a string of length 5. How many times is strlen called?
- A. 5
- B. 10
- C. 6
- D. 1
Correct answer: C. 6
The condition is tested 6 times (5 successful tests and one failing), and strlen runs at each test. Storing the length once avoids this.
More C Programming questions
- Which C code is correct and fast enough for finding whether a sorted array a of n = 2 x 10^5 ints has two elements summing to t?
- For N = 10^6 integers in the range 0 to 10^6, which sorting method is correct and fast enough?
- A queue is needed for 10^5 operations on an array. Which dequeue is correct and fast enough?
- Growing a dynamic array by a fixed 1 element every time it fills (capacity + 1) costs what in the worst case for N pushes?
- Which method is correct and fast enough for answering 10^5 range-sum queries on an array of 10^5 integers?
- Which method is correct and fast enough for n choose r modulo p = 10^9 + 7 for 10^6 queries with n up to 10^6?