×

C Tutorial

C Basics

C Data Types

C Input/Output

C Operators

C Conditional Statements

C Control Statements

C Strings

C Functions

C Arrays

C Structure and Unions

C Pointers

C Preprocessor Directives

C Command-line Arguments

C File Handlings

C Graphics

C Advance Topics

C Tips and Tricks

C Important Topics

C Practice

Removing trailing newline character from fgets() input

In thus tutorial, we are going to learn how can I remove trailing newline character from fgets() input in C? By Shubh Pachori Last updated : April 20, 2023

Input string using fgets() function

In C programming language, to read the whole string we can use the fgets() function rather than gets() function because the fgets() function bounds the reading input with the size of the character array but the gets() function reads the whole input which is given by the user even if it is greater than the memory located to it.

Syntax

char *fgets(char *str, int n, FILE *strarr)

Example

#include <stdio.h>

int main()
{
    char str[15];
    char str1[15];

    // Input string using gets()
    printf("Enter String 1: ");
    fflush(stdin);
    gets(str1);

    // Input string using fgets()
    printf("Enter String 2: ");
    fflush(stdin);
    fgets(str, 15, stdin);

    printf("gets() string is: %s\n", str1);

    printf("fgets() string is: %s\n", str);
    
    return 0;
}

Output:

Enter String 1: Shubh is a student
Enter String 2: Shubh is a student 
gets() string is: Shubh is a student
fgets() string is: Shubh is a stu
*** stack smashing detected ***: terminated

In above output, we can clearly see the difference between fgets() function and gets() function. The compiler also shows the warning for using the gets() function because it doesn't bound the limit of input by the size of the array of characters, i.e., string.

How to remove trailing newline character from fgets() input?

To remove trailing newline characters from the fgets() function, for that they are as follows, strcspn() function and strchr() function are those functions which can be used for this task all these functions are already present in the <string.h> header file.

Example

#include <stdio.h>

int main()
{
    char str[15];

    printf("Enter String: ");
    fflush(stdin);
    fgets(str, 15, stdin);

    printf("fgets() string is: %s\n", str);
    
    return 0;
}

Output:

Enter String: Shubh Pachori
fgets() string is: Shubh Pachori

In this output, we can see that there is some space after the printing of the string. To overcome this there are some functions to do so.

1) By using strcspn() function

The function calculates the length of the number of characters before the first occurrence of a character present in the string.

Example

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

int main()
{
    char str[15];

    printf("Enter the data = ");
    if (fgets(str, sizeof(str), stdin) == NULL) {
        printf("Fail to read the input stream");
    }
    else {
        str[strcspn(str, "\n")] = '\0';
    }

    printf("Entered Data = %s\n", str);
    
    return 0;
}

Output:

Enter the data = Shubh Pachori
Entered Data = Shubh Pachori

2) By using strchr() function

This function replaces the "\n" with the NULL in the string str. If "\n" exists in the string.

Example

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

int main()
{
    char str[15];

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

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

    return 0;
}

Output:

Enter the data = Shubh Pachori
Entered Data = Shubh Pachori



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.