草庐IT

关于 c:String 比较差异(适用于某些情况,有时不适用)

codeneng 2023-03-28 原文

String Comparison Discrepancy (Works for some cases and sometimes doesn't)

我使用以下代码测试了我收到的电子邮件文件中的一些文本检测。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <stdio.h>

int main()
{
  int max_size = 100;
  char email[100] ="aheanstring4strinstringzil.comg";
  int pointer = 0;
  int i = 1;
  int j = 0;
  int len = 0;
  int chk = 0;
  char found1[7] ="string1";
  char found2[7] ="string2";
  char found3[7] ="string3";
  char found4[7] ="string4";
  char found5[7] ="string5";

  char user1[7] ="string1"; // 23        
  char user2[7] ="string2";// 19        
  char user3[7] ="string3"; // 14        
  char user4[7] ="string4"; // 16      
  char user5[7] ="string5";; // 15

  while (pointer != max_size && chk != 1)
  {

    for (j = 0;j<7; j++)
    {
      found1[j] = *(email+pointer+j);
    }

    if (strcmp(found1, user1) == 0){
      printf("Authorized User Found\
"
);
      chk = 1;
      continue;
    }

    for (j = 0;j<7; j++)
    {
      found2[j] = *(email+pointer+j);
    }

    if (strcmp(found2, user2) == 0){
      printf("Authorized User Found\
"
);
      chk = 1;
      continue;
    }

    for (j = 0;j<7; j++)
    {
      found3[j] = *(email+pointer+j);
    }

    if (strcmp(found3, user3) == 0){
      printf("Authorized User Found\
"
);
      chk = 1;
      continue;
    }

    for (j = 0;j<7; j++)
    {
      found4[j] = *(email+pointer+j);
    }

    if (strcmp(found4, user4) == 0){
      printf("Authorized User Found\
"
);
      chk = 1;
      continue;
    }


    for (j = 0;j<7; j++)
    {
      found5[j] = *(email+pointer+j);
    }

    if (strcmp(found5, user5) == 0){
      printf("Authorized User Found\
"
);
      chk = 1;
      continue;
    }

    pointer++;

  }
  printf("Check is %d, Pointer is %d\
"
,chk, pointer);
  return 0;
}

我使用上述代码在电子邮件正文中查找特定用户。如果找到用户,while 循环将中断。当我尝试运行它时,我在上面的变量 (email)

中包含了不同的字符串

我首先尝试在不同的在线 C 编译器上运行它。他们都有字符串 1,3 和 5 工作正常。 (被检测到)

其中一些字符串 2 工作正常(被检测到)。

然而,他们都分享了 string2 从未被检测到的事实。我不知道为什么。我试着想一个原因,但不知道为什么。

非常感谢您的帮助。

  • [7] 更改为 []
  • 检查您的编译器警告(如果您没有看到任何编译器警告,请检查您的编译器警告设置)。 warning: implicit declaration of function 'strcmp':您没有包含 string.h。"它可以正常工作"只是运气,它可能不适用于其他功能。
  • 太感谢了。有效。


1
char found1[7] ="string1";

这里 found1 不是 C 中的有效字符串,因为没有 nul 终止。你需要有

1
char found1[8] ="string1";

您将 found1 传递给 strcmp(),这将导致未定义的行为,因为 strcmp() 需要一个以空字符结尾的字符串。

或者正如@Barak Manos 建议的那样,你可以去

1
char found1[] ="string1";

  • @Mohamed 如果我已经回答了你的问号,那就是已回答

有关关于 c:String 比较差异(适用于某些情况,有时不适用)的更多相关文章

  1. ruby - 将差异补丁应用于字符串/文件 - 2

    对于具有离线功能的智能手机应用程序,我正在为Xml文件创建单向文本同步。我希望我的服务器将增量/差异(例如GNU差异补丁)发送到目标设备。这是计划:Time=0Server:hasversion_1ofXmlfile(~800kiB)Client:hasversion_1ofXmlfile(~800kiB)Time=1Server:hasversion_1andversion_2ofXmlfile(each~800kiB)computesdeltaoftheseversions(=patch)(~10kiB)sendspatchtoClient(~10kiBtransferred)Cl

  2. ruby - Ruby 的 Hash 在比较键时使用哪种相等性测试? - 2

    我有一个围绕一些对象的包装类,我想将这些对象用作散列中的键。包装对象和解包装对象应映射到相同的键。一个简单的例子是这样的:classAattr_reader:xdefinitialize(inner)@inner=innerenddefx;@inner.x;enddef==(other)@inner.x==other.xendenda=A.new(o)#oisjustanyobjectthatallowso.xb=A.new(o)h={a=>5}ph[a]#5ph[b]#nil,shouldbe5ph[o]#nil,shouldbe5我试过==、===、eq?并散列所有无济于事。

  3. ruby - 默认情况下使选项为 false - 2

    这是在Ruby中设置默认值的常用方法:classQuietByDefaultdefinitialize(opts={})@verbose=opts[:verbose]endend这是一个容易落入的陷阱:classVerboseNoMatterWhatdefinitialize(opts={})@verbose=opts[:verbose]||trueendend正确的做法是:classVerboseByDefaultdefinitialize(opts={})@verbose=opts.include?(:verbose)?opts[:verbose]:trueendend编写Verb

  4. ruby - 在没有 sass 引擎的情况下使用 sass 颜色函数 - 2

    我想在一个没有Sass引擎的类中使用Sass颜色函数。我已经在项目中使用了sassgem,所以我认为搭载会像以下一样简单:classRectangleincludeSass::Script::FunctionsdefcolorSass::Script::Color.new([0x82,0x39,0x06])enddefrender#hamlengineexecutedwithcontextofself#sothatwithintemlateicouldcall#%stop{offset:'0%',stop:{color:lighten(color)}}endend更新:参见上面的#re

  5. ruby-on-rails - 在 Rails 和 ActiveRecord 中查询时忽略某些字段 - 2

    我知道我可以指定某些字段来使用pluck查询数据库。ids=Item.where('due_at但是我想知道,是否有一种方法可以指定我想避免从数据库查询的某些字段。某种反拔?posts=Post.where(published:true).do_not_lookup(:enormous_field) 最佳答案 Model#attribute_names应该返回列/属性数组。您可以排除其中一些并传递给pluck或select方法。像这样:posts=Post.where(published:true).select(Post.attr

  6. ruby - 字符串文字中的转义状态作为 `String#tr` 的参数 - 2

    对于作为String#tr参数的单引号字符串文字中反斜杠的转义状态,我觉得有些神秘。你能解释一下下面三个例子之间的对比吗?我特别不明白第二个。为了避免复杂化,我在这里使用了'd',在双引号中转义时不会改变含义("\d"="d")。'\\'.tr('\\','x')#=>"x"'\\'.tr('\\d','x')#=>"\\"'\\'.tr('\\\d','x')#=>"x" 最佳答案 在tr中转义tr的第一个参数非常类似于正则表达式中的括号字符分组。您可以在表达式的开头使用^来否定匹配(替换任何不匹配的内容)并使用例如a-f来匹配一

  7. ruby - 在不使用 RVM 的情况下在 Mac 上卸载和升级 Ruby - 2

    我最近决定从我的系统中卸载RVM。在thispage提出的一些论点说服我:实际上,我的决定是,我根本不想担心Ruby的多个版本。我只想使用1.9.2-p290版本而不用担心其他任何事情。但是,当我在我的Mac上运行ruby--version时,它告诉我我的版本是1.8.7。我四处寻找如何简单地从我的Mac上卸载这个Ruby,但奇怪的是我没有找到任何东西。似乎唯一想卸载Ruby的人运行linux,而使用Mac的每个人都推荐RVM。如何从我的Mac上卸载Ruby1.8.7?我想升级到1.9.2-p290版本,并且我希望我的系统上只有一个版本。 最佳答案

  8. ruby - 从 String#split 返回的零长度字符串 - 2

    在Ruby1.9.3(可能还有更早的版本,不确定)中,我试图弄清楚为什么Ruby的String#split方法会给我某些结果。我得到的结果似乎与我的预期相反。这是一个例子:"abcabc".split("b")#=>["a","ca","c"]"abcabc".split("a")#=>["","bc","bc"]"abcabc".split("c")#=>["ab","ab"]在这里,第一个示例返回的正是我所期望的。但在第二个示例中,我很困惑为什么#split返回零长度字符串作为返回数组的第一个值。这是什么原因呢?这是我所期望的:"abcabc".split("a")#=>["bc"

  9. ruby - 在什么情况下会使用 Sinatra 或 Merb? - 2

    我正在学习Rails,对Sinatra和Merb知之甚少。我想知道您会在哪些情况下使用Merb/Sinatra。感谢您的反馈! 最佳答案 Sinatra是一个比Rails更小、更轻的框架。如果你想让一些东西快速运行,只需发送几个URL并返回一些简单的内容,就可以使用它。看看Sinatrahomepage;这就是启动和运行“Hello,World”所需的全部内容,而在Rails中,您需要生成整个项目结构、设置Controller和View、设置路由等等(我还没有有一段时间写了一个Rails应用程序,所以我不知道“Hello,World

  10. ruby - 是否可以在不实际发送或读取数据的情况下查明 ruby​​ 套接字是否处于 ESTABLISHED 或 CLOSE_WAIT 状态? - 2

    s=Socket.new(Socket::AF_INET,Socket::SOCK_STREAM,0)s.connect(Socket.pack_sockaddr_in('port','hostname'))ssl=OpenSSL::SSL::SSLSocket.new(s,sslcert)ssl.connect从这里开始,如果ssl连接和底层套接字仍然是ESTABLISHED,或者它是否在默认值7200之后进入CLOSE_WAIT,我想检查一个线程几秒钟甚至更糟的是在实际上不需要.write()或.read()的情况下关闭。是用select()、IO.select()还是其他方法完成

随机推荐