0

Is there a way to remove the \N character from the end of the string reading with fgets, without using string processing functions (strlen, strtok, strcspn, etc) ? I tried that, but it doesn't work

char* remove_newline_char(char *str){
    for(int i = 0; i < str[i]; i++){
        if(str[i] == '\n'){
            str[i] = '\0';
        }
    }
    return str;
}
5
  • 10
    Please explain the condition i < str[i] to yout rubber duck. Commented May 9, 2022 at 6:50
  • @Someprogrammerdude ahhhhhh, I just realized what I did ..., thanks Commented May 9, 2022 at 6:56
  • @Someprogrammerdude Of course it had to be that way int i = 0; str[i]; i++ Commented May 9, 2022 at 6:57
  • Don't add "fixed" to the title. Instead press the checkmark next to the best answer below, or, if there's no good answer, post your own and mark it. Commented May 9, 2022 at 7:03
  • stackoverflow.com/questions/2693776/… Commented May 9, 2022 at 7:05

3 Answers 3

5

Your code is semantically incorrect. the loop termination expression i < str[i] says:

is the string index less than the character value at that index?

which is of course nonsense - the index and the character value are unrelated.

char* remove_newline_char(char* str)
{
    int i = 0 ;
    while( str[i] != 0 && str[i] != '\n' )
    {
        i++ ;
    }
    str[i] = 0 ;

    return str ;
}

Given that this processing is specific to buffers populated by fgets() rather then a generic string function, there may be merit in combining the input with the processing so that it will not be applied to inappropriate strings (i.e. not from fgets()):

char* freadline( char* str, int num, FILE* stream )
{
    char* ret = NULL ;

    if( (ret = fgets( str, num, str )) != NULL )
    {
        int i = 0 ;
        while( str[i] != 0 && str[i] != '\n' )
        {
            i++ ;
        }
        str[i] = 0 ;
     }
     return ret ;
}
Sign up to request clarification or add additional context in comments.

1 Comment

freadline() need not repeatedly test str[i] != '\n' in a loop. Only one test, after the loop needed. if (i > 0 && str[i - 1] == '\n') str[i-1] = 0;
5

You don't need to transverse through the full array and check for each character at every iteration.

As, you have stated don't use string functions, I'm not using strlen() function.

In fgets() newline character is present at length - 1 position.

char* remove_newline_char(char *str){
    size_t len = 0;
    while (str[len])
        len++;
    if(len > 0 && str[len - 1] == '\n')
        str[len - 1] = 0;
    return str;
}

Also, if you know the length of string, then you can avoid that while loop

char* remove_newline_char(char *str, size_t len){
    if(len > 0 && str[len - 1] == '\n')
        str[len - 1] = 0;
    return str;
}

Comments

0

Of course my loop was wrong. Here's the version that works

char* remove_newline_char(char *str){
for(int i = 0; str[i]; i++){
    if(str[i] == '\n'){
        str[i] = '\0';
    }
}
return str;

}

1 Comment

If you fix it that way, you are running to the end of the original string when you only need to run until \n - a minor issue in the case of fgets() populated strings perhaps, but your function could be presented with strings not from fgets().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.