Создайте свою собственную функцию сравнения, которая игнорирует \n
или любой другой символ, который вы передаете:
int strcmp_ignoring_char(const char* s1, const char* s2, const char ignore)
{
while ( *s1 != '\0' && *s1 != '\0' )
{
if ( *s1 == ignore )
{
s1++;
continue;
}
if ( *s2 == ignore )
{
s2++;
continue;
}
if ( *s1 != *s2 )
return *s1 > *s2 ? 1 : -1;
s1++;
s2++;
}
/* modified to account for trailing ignore chars, as per Lundin comment */
if ( *s1 == '\0' && *s2 == '\0' )
return 0;
const char* nonEmpty = *s1 == '\0' ? s2 : s1;
while ( *nonEmpty != '\0' )
if ( *nonEmpty++ != ignore )
return 1;
return 0;
}
Таким образом, вы не будете сканировать строки дважды.
Вытакже может создать вариант, который игнорирует строку, а не один символ:
int strcmp_ignoring_char(const char* s1, const char* s2, const char* ignore)