ЁЯУв Too many exams? DonтАЩt know which one suits you best? Book Your Free Expert ЁЯСЙ call Now!

  • google app store apple app store
  • тЬЦ

      Question

      Complete the C function to copy at most n characters

      from source starting at start_index into destination, ensuring destination is null-terminated. Assume destination has enough allocated memory. #include #include // For debugging, not strictly needed void safe_copy_substring(char* destination, const char* source, int start_index, int n) { ┬а ┬а int source_len = strlen(source); ┬а ┬а if (start_index >= source_len) { ┬а ┬а ┬а ┬а destination[0] = '\0'; // Source index out of bounds, return empty string ┬а ┬а ┬а ┬а return; ┬а ┬а } ┬а ┬а int chars_to_copy = n; ┬а ┬а if (start_index + chars_to_copy > source_len) { ┬а ┬а ┬а ┬а chars_to_copy = source_len - start_index; ┬а ┬а } ┬а ┬а strncpy(destination, _________, chars_to_copy); // Line to complete ┬а ┬а destination[chars_to_copy] = '\0'; // Ensure null-termination }
      A source Correct Answer Incorrect Answer
      B source + start_index Correct Answer Incorrect Answer
      C &source[start_index] Correct Answer Incorrect Answer
      D source[start_index] Correct Answer Incorrect Answer
      E Both B and C are correct. Correct Answer Incorrect Answer

      Solution

      Correct Answer: E (Both source + start_index and &source[start_index] correctly provide a pointer to the starting character of the substring within source.)

      Practice Next
      More IT Operating System Questions
      ask-question