Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C中是否有一个库函数返回两个字符串中第一个不匹配字符的位置?_C_C Strings - Fatal编程技术网

C中是否有一个库函数返回两个字符串中第一个不匹配字符的位置?

C中是否有一个库函数返回两个字符串中第一个不匹配字符的位置?,c,c-strings,C,C Strings,我有两条大的弦 char str1[] = "letsgosomewhereandfindsomethingnew"; char str2[] = "letsgosomewhereandfindcomethingnew"; 我想获得字符串的第一个不匹配字符,前提是字符串长度相同。大多数库cstring比较都设置为排序。你在寻找不同 因此,以最好的方式,推出您自己的: char *p1 = str1; char *p2 = str2; while (*p1 && *p2 &am

我有两条大的弦

char str1[] = "letsgosomewhereandfindsomethingnew";
char str2[] = "letsgosomewhereandfindcomethingnew";

我想获得字符串的第一个不匹配字符,前提是字符串长度相同。

大多数库cstring比较都设置为排序。你在寻找不同

因此,以最好的方式,推出您自己的:

char *p1 = str1;
char *p2 = str2;

while (*p1 && *p2 && (*p1 == *p2))
{
    ++p1;
    ++p2;
}
// p1 and p2 should point to the two different characters (strings match if *p1 == *p2)
未经测试的动态编码

或者根据评论进行优化

char *p1 = str1;
char *p2 = str2;

while (*p1 && (*p1 == *p2))
{
    ++p1;
    ++p2;
}
// p1 and p2 should point to the two different characters (strings match if *p1 == *p2)
由于另一条评论现在要求补偿

int offset;

for (offset = 0; str1[offset] && (str1[offset] == str2[offset]); ++offset)
{
    // Empty block because I hate for statements without bodies
}
// Now you have the number of matching characters in offset

大多数库cstring比较都设置为排序。你在寻找不同

因此,以最好的方式,推出您自己的:

char *p1 = str1;
char *p2 = str2;

while (*p1 && *p2 && (*p1 == *p2))
{
    ++p1;
    ++p2;
}
// p1 and p2 should point to the two different characters (strings match if *p1 == *p2)
未经测试的动态编码

或者根据评论进行优化

char *p1 = str1;
char *p2 = str2;

while (*p1 && (*p1 == *p2))
{
    ++p1;
    ++p2;
}
// p1 and p2 should point to the two different characters (strings match if *p1 == *p2)
由于另一条评论现在要求补偿

int offset;

for (offset = 0; str1[offset] && (str1[offset] == str2[offset]); ++offset)
{
    // Empty block because I hate for statements without bodies
}
// Now you have the number of matching characters in offset


我相信您正在查找strcmp。@500 InternalServerError-但它不会返回第一个不匹配的字符
strcmp
根据词典比较返回<0、0或>0。很抱歉这么说,但是这个问题为什么会得到CV,原因是工具记录?这是一个关于库函数的问题(除非明确提到,这是标准库),而不是一个工具。@500 InternalServerError-据我所知,大小不是第一个不匹配的位置,在
“aabc”
“aacb”
中的“第一个不匹配字符”是什么,是
'c'
还是
'b'
?我相信您正在查找strcmp。@500 InternalServerError-但它不会返回第一个不匹配的字符
strcmp
根据词典比较返回<0、0或>0。很抱歉这么说,但是这个问题为什么会得到CV,原因是工具记录?这是一个关于库函数的问题(除非明确提到,这是标准库),而不是一个工具。@500 InternalServerError-据我所知,大小不是第一个不匹配的位置,在
“aabc”
“aacb”
中的“第一个不匹配字符”是什么,它是
'c'
还是
'b'
&&*p2
可以删除;这是多余的。如果p1=“abc”和p2=“abcd”,则
*p1&&*p1==*p2
将起作用(
*p1
为false);如果p1=“abcd”和p2=“abc”,则
*p1&&*p1===p2
将起作用(
*p1===p2
为false)。如果字符串不一致,则应谨慎行事sizes@infixed:如果尺寸不同,
*p1==*p2
在到达较短字符串的结尾时将已为false。我想
str2
的零终止符也会停止它。所以,好的,多余的结尾的评论不太正确。当两个字符串相等时,它们也可以指向空终止符。
&&&p2
可以删除;这是多余的。如果p1=“abc”和p2=“abcd”,则
*p1&&*p1==*p2
将起作用(
*p1
为false);如果p1=“abcd”和p2=“abc”,则
*p1&&*p1===p2
将起作用(
*p1===p2
为false)。如果字符串不一致,则应谨慎行事sizes@infixed:如果尺寸不同,
*p1==*p2
在到达较短字符串的结尾时将已为false。我想
str2
的零终止符也会停止它。所以,好的,多余的结尾的评论不太正确。当两个字符串相等时,它们也可以指向空终止符。