C语言 在 C 中逐字符比较两个字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24063156/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Compare two strings character by character in C
提问by user3605367
I have one 'simple' (I hope) question. I am actually coding some little program and I need to compare two strings, same length, but different letters like
我有一个“简单”(我希望)的问题。我实际上正在编写一些小程序,我需要比较两个长度相同但字母不同的字符串,例如
Eagle
and
和
Hdjoh
I want to compare the first letter of the first string with the first letter of the second string, the second letter of the first string with the second letter of the second string etc..
我想比较第一个字符串的第一个字母和第二个字符串的第一个字母,第一个字符串的第二个字母和第二个字符串的第二个字母等等。
I started to do like this:
我开始这样做:
for(i=0, i<N, i++){
for(j=0, j<N, j++){
if(string1[i]==string1[j] etc.. etc..
}
}
I see clearly that it doesn't compare first letter with first letter, second with second etc..
我清楚地看到它没有将第一个字母与第一个字母、第二个与第二个字母等进行比较。
So maybe anyone have an idea how can I do this? (Without using any functions of string.h, i want to do this ''on my own'').
所以也许有人知道我该怎么做?(不使用 string.h 的任何函数,我想“自己做”)。
Maybe its a stupid question but im still a novice in C so...
也许这是一个愚蠢的问题,但我仍然是 C 的新手,所以......
Ah and the last thing, I define the two strings with 5 characters in my example, but it could be more than 5 vs 5..
啊,最后一件事,我在示例中用 5 个字符定义了两个字符串,但它可能超过 5 对 5..
Thanks by advance for the ideas.
预先感谢您的想法。
Edit 1 :
编辑1:
#include <stdio.h>
#define N 20
int main()
{
unsigned char string1[N], string2[N];
int Answer=0, i=0;
scanf("%s", string1);
scanf("%s", string2);
for(i=0; i<N; i++){
if(string1[i]==string2[i]){
Answer=1;
}
else{
Answer=0;
}
}
printf("Answer = %d", Answer);
return 0;
}
回答by Kartik_Koro
Why are you using a nested for loop for this? If both strings are of size n do this:
为什么要为此使用嵌套的 for 循环?如果两个字符串的大小均为 n,请执行以下操作:
for(int i=0;i<n;i++){
if(string1[i]==string2[i]){
//do something
else if(// lesser than condition)
//do something else
else if(//greater than condition)
//do something else other than the previous something
}
Here you when i=0, you are comparing string1[0]with string2[0], when i=1, you compare string1[1]with string2[1]and so on.....
在这里,您时i=0,您比较string1[0]与string2[0]时i=1,你比较string1[1]有string2[1]等等.....
回答by unwind
Your approach with nested loops isn't very well thought-out.
您使用嵌套循环的方法不是经过深思熟虑的。
Clearly it will compare allletters of the second string against the first letter of the first string, then do the same for the second letter of the first string, and so on. Not at all the desired behavior.
显然,它将第二个字符串的所有字母与第一个字符串的第一个字母进行比较,然后对第一个字符串的第二个字母执行相同的操作,依此类推。根本不是想要的行为。
Re-implementing strcmp()isn't very hard, here's a shot:
重新实现strcmp()并不是很难,这里是一个镜头:
int my_strcmp(const char *a, const char *b)
{
for(; *a && *b && *a == *b; ++a, ++b)
;
if(*a < *b)
return -1;
return *a > *b;
}
Note that it returns zerowhen the strings are equal. A good way to write a test is:
请注意,当字符串相等时它返回零。编写测试的一个好方法是:
if(my_strmcp(a, b) == 0)
{
printf("two equal strings: '%s' and '%s'\n", a, b);
}
Some people write it as if(!my_strcmp())but I don't recommend that, since it's mashing up so many concepts.
有些人把它写成if(!my_strcmp())但我不建议这样做,因为它混合了很多概念。
回答by Jens
You want to use the same index for both strings to compare:
您想对两个字符串使用相同的索引进行比较:
unsigned len = strlen(s1);
assert(len == strlen(s2) && "Strings not the same length");
for (unsigned i = 0; i < len; i += 1)
{
if (s1[i] != s2[i])
return false; /* strings are not equal */
}
return true; /* strings are equal */
Make sure that the strings have the same encoding, either ASCIIor UTF8or whatever. Comparing strings of different encoding will cause trouble :)
回答by Avery
This code compares character by character. Note that this is not suitable for crypto code as it is vulnerable to a timing attack
此代码逐字符比较。请注意,这不适用于加密代码,因为它容易受到计时攻击
for(i=0; i<N; i++){
if(string1[i]==string2[i]){
equal = 1;
}else{
equal = 0;
break;
}
}
Notes:
I am assuming same length (as stated in question)
I am also assuming strings are non-zero length
注意:
我假设长度相同(如问题所述)
我还假设字符串长度不为零
Both of these assumptions may not be true in other code.
这两个假设在其他代码中可能都不成立。
回答by chux - Reinstate Monica
Simple compare each element until the end of string is found or a difference.
简单地比较每个元素,直到找到字符串的结尾或差异。
size_t i = 0;
while (string1[i] != 'int diff_bits = 0;
for(size_t i=0; i<N; i++) {
diff_bits |= string1[i] ^ string2[i];
}
int equal = diff_bits == 0;
' && string1[i] == string2[j]) i++;
int StringTheSame = string1[i] == string2[j];
This ignores N, but stops when either end-of-string ('\0') is encountered.
这将忽略N,但在遇到任一字符串结尾 ( '\0')时停止。
[Edit] @Kartik_Koro suggested a concern about a timing attack. Following is a constant time solution
[编辑] @Kartik_Koro 提出了对计时攻击的担忧。以下是恒定时间解决方案
##代码##The above has a problem if either string's length is shorted than N-1, but per OP's requirements, that shouldnot happen.
如果任一字符串的长度比 N-1 短,则上述内容会出现问题,但根据 OP 的要求,不应发生这种情况。

