Home / Questions / Is there any inbuilt library function in C to remove leading and trailing spaces from a string? How will you remove them in C?
Explanatory Question

Is there any inbuilt library function in C to remove leading and trailing spaces from a string? How will you remove them in C?

👁 608 Views
📘 Detailed Answer
🕒 Easy to Read
Read the answer carefully and go through the related questions on the right side to improve your understanding of this topic.

Answer with Explanation

  • There is no inbuilt function to remove leading and trailing spaces from a string in C. We need to write our own function to remove them.
  • We need to check first non-space character in given string. Then, we can copy that string from where non space character is found. Then, we can check whether any spaces are available in copied string from the end of the string. If space is found, we can copy ‘\0’ in that space until any character is found. Because, ‘\0’indicates the end of the string. Now, we have removed leading and trailing spaces in a given string.