How to remove trailing newline character from fgets input

How to remove trailing newline character from fgets input?

There are many ways to remove the trailing newline character from fgets input. Below I am mentioning some ways to remove the trailing newline character from the fgets input. You can use any one of them as per your requirement.

But here I want to mention that in the below solution some are the compliant solution and some are the non-compliant solutions.

 

Method 1:

 

#include <stdio.h>
#include <string.h>

#define  BUFFER_SIZE 24

int main(void)
{
    char buf[BUFFER_SIZE];

    printf("Enter the data = ");

    if (fgets(buf, sizeof(buf), stdin) == NULL)
    {
        printf("Fail to read the input stream");
    }
    else
    {
        buf[strlen(buf) - 1] = '\0';
    }

    printf("Entered Data = %s\n",buf);

    return 0;
}

 

Explanation: The strlen() function computes the length of a string by determining the number of characters that precede the terminating null character. A problem occurs if the first character read from the input by fgets() happens to be a null character. This may occur, for example, if a binary data file is read by the fgets(). If the first character in buf is a null character, strlen(buf) returns 0, the expression strlen(buf) – 1 wraps around to a large positive value, and a write-outside-array-bounds error occurs.

 

Method 2:

 

#include <stdio.h>
#include <string.h>

#define  BUFFER_SIZE 24

int main(void)
{
    char buf[BUFFER_SIZE];

    printf("Enter the data = ");

    if (fgets(buf, sizeof(buf), stdin) == NULL)
    {
        printf("Fail to read the input stream");
    }
    else
    {
        //find new line
        char *ptr = strchr(buf, '\n');
        if (ptr)
        {
            //if new line found replace with null character
            *ptr  = '\0';
        }
    }
    printf("Entered Data = %s\n",buf);

    return 0;
}

Explanation: In the above C program strchr()  (string function) replace the newline character in the string with ‘\0’ if it exists.

 

 

Method 3:

 

#include <stdio.h>
#include <string.h>

#define  BUFFER_SIZE 24

int main(void)
{
    char buf[BUFFER_SIZE];

    printf("Enter the data = ");

    if (fgets(buf, sizeof(buf), stdin) == NULL)
    {
        printf("Fail to read the input stream");
    }
    else
    {
        buf[strcspn(buf, "\n")] = '\0';
    }
    printf("Entered Data = %s\n",buf);

    return 0;
}

Explanation: The strcspn() calculates the length of the number of characters before the 1st occurrence of character present in both the string. Because we have given “\n” as a second-string so we will get the length of the string before the “\n”. Now we put the ‘\0’ in place of ‘\n’.

 

 

Method 4:

 

#include <stdio.h>
#include <string.h>

#define  BUFFER_SIZE 24

int main(void)
{
    char buf[BUFFER_SIZE];

    printf("Enter the data = ");

    if (fgets(buf, sizeof(buf), stdin) == NULL)
    {
        printf("Fail to read the input stream");
    }
    else
    {
        strtok(buf,"\n");
    }
    printf("Entered Data = %s\n",buf);

    return 0;
}

 

Note: strtok is not safe to use. You can use strtok_r in place of strtok.

 

 

Method 5:

 

#include <stdio.h>
#include <string.h>

#define  BUFFER_SIZE 24

void removeNewLineChar(char *ptr)
{
    while((ptr != NULL) && (*ptr != '\n'))
    {
        ++ptr;
    }

    *ptr = '\0';
}

int main(void)
{
    char buf[BUFFER_SIZE];

    printf("Enter the data = ");

    if (fgets(buf, sizeof(buf), stdin) == NULL)
    {
        printf("Fail to read the input stream");
    }
    else
    {
      removeNewLineChar(buf);
    }
    printf("Entered Data = %s\n",buf);

    return 0;
}

 

Explanation: In this C program I am finding the position of ‘\n’ using the while loop and placing the null character ( ‘\0’). But you need to use this code very carefully.

 

Note: Some of the compliant solution and some Noncompliant solution. It is up to you which you want to use in your C program.

 

Recommended Articles for you:



Reference: Check this Link.