Which expression returns the length of a string s in the palindrome code?

Prepare for the FAST Enterprises IC Interview. Enhance your skills with flashcards and multiple-choice questions. Each question provides hints and detailed explanations. Excel in your interview!

Multiple Choice

Which expression returns the length of a string s in the palindrome code?

Explanation:
The expression length(s) is the best fit because the palindrome code uses a function named length that takes a string as its argument and returns how many characters it contains. Calling this function directly with the string, as length(s) does, matches that API and gives you the numeric length you need. Explain briefly why the others aren’t the right match in this context: strlen(s) is a C-style function for C strings, not a general string object in many languages. s.length implies a property or a parameterless method, which doesn’t align with calling a function named length. s.size() is a common member function in languages like C++ for certain string types, but it’s a different API than the length function used in this code. The key idea is to use the API that the palindrome code defines for obtaining length, which is the length function.

The expression length(s) is the best fit because the palindrome code uses a function named length that takes a string as its argument and returns how many characters it contains. Calling this function directly with the string, as length(s) does, matches that API and gives you the numeric length you need.

Explain briefly why the others aren’t the right match in this context: strlen(s) is a C-style function for C strings, not a general string object in many languages. s.length implies a property or a parameterless method, which doesn’t align with calling a function named length. s.size() is a common member function in languages like C++ for certain string types, but it’s a different API than the length function used in this code. The key idea is to use the API that the palindrome code defines for obtaining length, which is the length function.

Subscribe

Get the latest from Passetra

You can unsubscribe at any time. Read our privacy policy