[Solved] Cutting ‘ \0’ from strings [closed]


Your problem is evident in your debug output. getline does not strip the newline from the input, so for example you are searching for:

"know\n" 

in

"I do not know what to write\n"

So your problem is not about stripping the \0 string terminator, but rather stripping the \n line-end.

That can be achieved in a number of ways, for example:

char* newline = strrchr( line, '\n' ) ;
if( newlineaddr != NULL )
{
    *newlineaddr="\0" ;
}

or

size_t newlineindex = strcspn(line, "\n") ;
line[newlineindex] = '\0' ;

The first copes with multi-line input (not needed in this case) – only removing the last newline, while the second is more succinct.

5

solved Cutting ‘ \0’ from strings [closed]