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 ;
}