正在检查字符串-C中是否存在\n
我试图比较两个字符串,尽管它们看起来一样,但我没有找到匹配项。结果显示一个字符串包含\n 所以我的问题是,是否有一种方法可以检查字符串是否包含“\n”? 我正在使用strcmp函数正在检查字符串-C中是否存在\n,c,string,C,String,我试图比较两个字符串,尽管它们看起来一样,但我没有找到匹配项。结果显示一个字符串包含\n 所以我的问题是,是否有一种方法可以检查字符串是否包含“\n”? 我正在使用strcmp函数 char *tempData; char *checkThis; tempData = "Hello \n"; checkThis = "Hello"; if(strcmp(tempData, checkThis) == 0) { printf("Match"); } 您可以在比较之前去掉空白,这样就不需要检查
char *tempData;
char *checkThis;
tempData = "Hello \n";
checkThis = "Hello";
if(strcmp(tempData, checkThis) == 0)
{
printf("Match");
}
您可以在比较之前去掉空白,这样就不需要检查“\n”。但是您可以只比较字符串,假设这是您想要做的
有一些关于如何在C中执行此操作的答案。您可以在比较之前去掉空白,然后不需要检查“\n”。但是您可以只比较字符串,假设这是您想要做的
有一些关于如何在C中执行此操作的答案。创建自己的比较函数,该函数忽略
\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)
创建您自己的比较函数,该函数忽略传入的
\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)
这是我的尝试。除了C99的功能外,我一直试图保持它与MISRA-C兼容
#include <stdint.h>
#include <stdbool.h>
#include <ctype.h>
#include <stdio.h>
int8_t strcmp_ignore_space (const uint8_t* s1, const uint8_t* s2)
{
while ((*s1 != '\0') && (*s2 != '\0'))
{
bool space1 = isspace(*s1);
bool space2 = isspace(*s2);
if(space1)
{
s1++;
}
if(space2)
{
s2++;
}
if (!space1 && !space2)
{
if (*s1 != *s2)
{
break;
}
else
{
s1++;
s2++;
}
}
} // while ((*s1 != '\0') && (*s2 != '\0'))
if(*s1 != '\0') // remove trailing white spaces
{
while(isspace(*s1))
{
s1++;
}
}
if(*s2 != '\0') // remove trailing white spaces
{
while(isspace(*s2))
{
s2++;
}
}
return (int8_t)( (int16_t)*s1 - (int16_t)*s2 );
}
int main()
{
// obscure strings with various white space characters, but otherwise equal
if(strcmp_ignore_space(" He\vllo \n",
"\r He\fll o ") == 0)
{
printf("Same\n");
}
else
{
printf("Different\n");
}
return 0;
}
#包括
#包括
#包括
#包括
int8\t strcmp\u ignore\u空间(const uint8\u t*s1,const uint8\u t*s2)
{
而((*s1!='\0')&(*s2!='\0'))
{
布尔空间1=isspace(*s1);
布尔空间2=isspace(*s2);
如果(空格1)
{
s1++;
}
如果(空格2)
{
s2++;
}
如果(!space1&&!space2)
{
如果(*s1!=*s2)
{
打破
}
其他的
{
s1++;
s2++;
}
}
}//而((*s1!='\0')&(*s2!='\0'))
if(*s1!='\0')//删除尾随空格
{
while(isspace(*s1))
{
s1++;
}
}
if(*s2!='\0')//删除尾随空格
{
while(isspace(*s2))
{
s2++;
}
}
报税表(整数倍)(整数倍)*s1-(整数倍)*s2);
}
int main()
{
//使用各种空格字符隐藏字符串,但在其他方面相等
如果(strcmp)忽略空间(“He\vllo\n”,
“\r他\fl o”)==0)
{
printf(“相同的\n”);
}
其他的
{
printf(“不同的\n”);
}
返回0;
}
以下是我的尝试。除了C99的功能外,我一直试图保持它与MISRA-C兼容
#include <stdint.h>
#include <stdbool.h>
#include <ctype.h>
#include <stdio.h>
int8_t strcmp_ignore_space (const uint8_t* s1, const uint8_t* s2)
{
while ((*s1 != '\0') && (*s2 != '\0'))
{
bool space1 = isspace(*s1);
bool space2 = isspace(*s2);
if(space1)
{
s1++;
}
if(space2)
{
s2++;
}
if (!space1 && !space2)
{
if (*s1 != *s2)
{
break;
}
else
{
s1++;
s2++;
}
}
} // while ((*s1 != '\0') && (*s2 != '\0'))
if(*s1 != '\0') // remove trailing white spaces
{
while(isspace(*s1))
{
s1++;
}
}
if(*s2 != '\0') // remove trailing white spaces
{
while(isspace(*s2))
{
s2++;
}
}
return (int8_t)( (int16_t)*s1 - (int16_t)*s2 );
}
int main()
{
// obscure strings with various white space characters, but otherwise equal
if(strcmp_ignore_space(" He\vllo \n",
"\r He\fll o ") == 0)
{
printf("Same\n");
}
else
{
printf("Different\n");
}
return 0;
}
#包括
#包括
#包括
#包括
int8\t strcmp\u ignore\u空间(const uint8\u t*s1,const uint8\u t*s2)
{
而((*s1!='\0')&(*s2!='\0'))
{
布尔空间1=isspace(*s1);
布尔空间2=isspace(*s2);
如果(空格1)
{
s1++;
}
如果(空格2)
{
s2++;
}
如果(!space1&&!space2)
{
如果(*s1!=*s2)
{
打破
}
其他的
{
s1++;
s2++;
}
}
}//而((*s1!='\0')&(*s2!='\0'))
if(*s1!='\0')//删除尾随空格
{
while(isspace(*s1))
{
s1++;
}
}
if(*s2!='\0')//删除尾随空格
{
while(isspace(*s2))
{
s2++;
}
}
报税表(整数倍)(整数倍)*s1-(整数倍)*s2);
}
int main()
{
//使用各种空格字符隐藏字符串,但在其他方面相等
如果(strcmp)忽略空间(“He\vllo\n”,
“\r他\fl o”)==0)
{
printf(“相同的\n”);
}
其他的
{
printf(“不同的\n”);
}
返回0;
}
是的,删除空格是所描述的开始任务主题的最佳变体。是的,删除空格是所描述的开始任务主题的最佳变体。我喜欢这个想法,但如果找到一个忽略
-字符,您的示例将永远不会结束。由于未更新s1
和/或s2
。。。此外,在while语句中,您检查了两次s1
。@Veger您完全正确。我将更改我的答案。它包含另一个错误:例如,如果*s1==ignore
s2不应增加(反之亦然),谢谢。我是C新手,'0是什么?“1”和“1:-1”是什么意思?它是说如果s1=s2,那么为真(0)或者为假1?它是一个三元if:boolean\u表达式?A:B
如果布尔_表达式为真,则返回A,否则返回B。我喜欢这个想法,但如果找到A忽略
-char,您的示例将永远不会结束。由于未更新s1
和/或s2
。。。此外,在while语句中,您检查了两次s1
。@Veger您完全正确。我将更改我的答案。它包含另一个错误:例如,如果*s1==ignore
s2不应增加(反之亦然),谢谢。我是C新手,'0是什么?“1”和“1:-1”是什么意思?它是说如果s1=s2,那么为真(0)或者为假1?它是一个三元if:boolean\u表达式?A:B
如果布尔_表达式为真,则返回A,否则返回B。不起作用,它应该返回什么?尝试使用printf(“%d\n”,strcmp\u ignore\u空格(“Ciao”,“Ciao”))
并返回-32
@vulkaninostrcmp函数返回大于、等于或小于零的整数,因为s1指向的字符串大于、等于或小于s2指向的字符串。
'C'-'C'=67-99=-32。所以它的行为是正确的。看起来像是一个漂亮而简单的实现:)不起作用,它应该返回什么?尝试使用printf(“%d\n”,strcmp\u ignore\u空格(“Ciao”,“Ciao”))
并返回-32
@vulkaninostrcmp函数返回大于、等于或小于零的整数,因为s1指向的字符串大于、等于或小于s2指向的字符串。
'C'-'C'=67-99=-32。所以它的行为是正确的。看起来像一个漂亮而简单的实现:)