这个函数有什么作用?

发布于 2024-09-29 18:48:59 字数 377 浏览 0 评论 0原文

int mystery( const char *s1, const char *s2 ) {
  for( ; *s1 != '\0' && *s2 != '\0'; s1++, s2++ ) {
    if( *s1 != *s2 ) {
      return 0;
    } //end if
  } //end for
  return 1;
}

我知道它有打字错误,但事实确实如此。 谢谢大家,我也需要它运行,并且我已经添加了变量声明,但我收到编译器错误,提示

In function `int main()':
错误:在“{”标记之前不允许使用函数定义
错误:预期
,'或';'在“{”标记之前

int mystery( const char *s1, const char *s2 ) {
  for( ; *s1 != '\0' && *s2 != '\0'; s1++, s2++ ) {
    if( *s1 != *s2 ) {
      return 0;
    } //end if
  } //end for
  return 1;
}

I know it has typing errors but this is exactly how it was.
thanks guys i need it to run too and i already added variable declaration but im getting compiler error that says

In function `int main()':
error: a function-definition is not allowed here before '{' token
error: expected
,' or `;' before '{' token

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

小草泠泠 2024-10-06 18:48:59

它比较两个字符串,如果字符串一以字符串二开头,则返回 1,反之亦然,如果不是,则返回 0

It compares two strings, returning 1 if string one starts with string two, or vice-versa and 0 if not.

亣腦蒛氧 2024-10-06 18:48:59

如果 (s1, s2) 中较短的一个与较长的一个的开头不同,则返回 0。字符串的长度可以不同,但​​其中一个字符串必须是从另一个字符串的开头开始的子字符串。

编辑。哎呀,夏斯比我先一步。在我之前投票给他。

It returns 0 if the shorter of (s1, s2) is not the same as the beginning of the longer one. The strings can be different lengths, but one must be a substring starting at the beginning of the other.

Edit. Oops sharth beat me to it. Vote him up before me.

小巷里的女流氓 2024-10-06 18:48:59

值得解释吗?

 for( ; *s1 != '\0' && *s2 != '\0'; s1++, s2++ ) {

for 循环中的第一个元素,位于第一个“;”之前进行初始设置,这里不需要。
因此,当 s1 和 s2 指向的字符不为零时,for 循环就会运行。在 C 和 C++ 中零标记字符串的结尾。
for 循环的最后一部分是在每个循环上执行的额外操作 - 在本例中,移动指针 s1 和 s2 以指向每个字符串中的下一个字符。

   if( *s1 != *s2 ) {
      return 0;

如果 s1 和 s2 指向的字符不相同 - 即我们在两个字符串中找到了第一个不同的字符,则返回 0 即 false

return 1;

如果我们到达其中一个字符串的末尾并且没有找到任何字符不同的字符返回 1 - 即 true。

因此,如果字符串相同或一个字符串以另一个字符串开头,则该函数返回 true,如果字符串具有不同的字符,则该函数返回 false。

Is it worth explaining?

 for( ; *s1 != '\0' && *s2 != '\0'; s1++, s2++ ) {

The first element in a for loop, before the first ';' does the initial setup, here none is required.
So the for loop runs while either of the characters pointed at by s1 and s2 are not zero. Zero marks the end of string in c and c++.
The last part of the for loop is what extra to do on each loop - in this case moves the pointers s1 and s2 to point to the next character in each of the strings.

   if( *s1 != *s2 ) {
      return 0;

If the characters pointed at by s1 and s2 aren't the same - ie we have found the first different character in the two strings, return 0 ie false

return 1;

If we get to the end of one of the strings and we haven't found any characters that were different return 1 - ie true.

So the function returns true if the strings are identical or one string begins with the other, and false is the strings have and different characters.

一笔一画续写前缘 2024-10-06 18:48:59

这段代码在 Java 中看起来像这样
<代码>

   int specificComparison(String s1, String s2){
        int minLength = Math.min(s1.length(), s2.length());
        if(s1.substring(0, minLength).equals(s2.substring(0, minLength)){
            return 1;
        }else{
            return 0;
        }
   }

This code would be looked like this in Java

   int specificComparison(String s1, String s2){
        int minLength = Math.min(s1.length(), s2.length());
        if(s1.substring(0, minLength).equals(s2.substring(0, minLength)){
            return 1;
        }else{
            return 0;
        }
   }

メ斷腸人バ 2024-10-06 18:48:59

看起来,当两个字符串相等时,该函数返回 1;当两个字符串不相等时,该函数返回 0。

至少,这可能是意图。

Looks like this function returns 1 when the two strings are equal and 0 when they are not.

At least, this may have been the intention.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文