fgets() reads a line from the required stream and shops it into the string pointed by str. It stops when both (n – 1) characters are learn, the newline character is learn, or the end-of-file is reached, whichever comes first.
Nonetheless, fgets() additionally reads the trailing newline character and finally ends up returning the information string adopted by ‘n’. So, on this article, we’re going to see some strategies to take away trailing newline characters from fgets() enter.
Strategies to take away trailing newline characters from fgets() enter
Technique 1(Utilizing strcspn() operate):
It’s a C library operate that calculates the size of the variety of characters earlier than the first prevalence of a personality current in each the strings.
Syntax:
str [ strcspn (str, “n”)] = 0;
Parameters:
str : The string wherein knowledge has been saved utilizing fgets() i.e.(char *fgets(char *str, int n, FILE *stream))
“n”: The character until which the size ought to be calculated
As a result of now we have given “n” as a second-string so we’ll get the size of the string earlier than the “n”. Now put the ‘