【问题标题】:C boolean function comparing 2 Strings alphabeticlyC布尔函数按字母顺序比较2个字符串
【发布时间】:2017-12-02 22:43:37
【问题描述】:

所以我试图比较 2 个字符串,如果第一个 (x) 小于第二个,则 twoStrComp 方法应返回 true,否则应为 false。

这是我的代码,虽然当我尝试在终端上运行它时,什么都没有出现... 好像它忽略了我的代码.. 另外我想知道指针是否会更有效,但我不确定如何声明它们,我可以做 twoStrComp(*x,*y) 吗? 原始代码:

#include <stdio.h>
#include <stdbool.h>

bool twoStrComp(char[], char[]);

int main(void)
{
  char x[]= "test";
  char y[]= "another";
  twoStrComp(x,y);      
}

bool twoStrComp(char string1[], char string2[] )
{
  int i=0, flag=0;    
  while(flag==0)
  {
    if (string1[i]<string2[i])
    {
      flag=-1;
    }
    else if (string1[i]==string2[i])
    {
      flag=0;
      i++
    }
    else
    {
      return 1;
    }
  }         
  return flag;
}  

新版本:

bool twoStrComp(char[], char[]);

int main(void)
{
    char x[]= "atest";
    char y[]= "another";
    bool ans = twoStrComp(x,y);
    printf("%s", ans ? "true" : "false");

}

bool twoStrComp(char string1[], char string2[] )
{
    int i=0, flag=0;
    bool value = false;
       while(flag==0) {
        if (string1[i]>string2[i])
        {
            flag=1;
            value = false;
        }
        else if (string1[i]<string2[i])
        {
            flag=-1;
            value = true;
        }
        else if (string1[i]==string2[i])
        {
            return 0;
            value = true;
        }
        else
        {
            i++;
        }
    }
    if(flag == 1)
        return (value == false);
    if(flag == -1)
        return (value == true);
 return value;
}

【问题讨论】:

  • 您正在调用twoStrComp,然后您将丢弃结果并静默退出。你想做什么呢?也许打印一些东西?你知道printf怎么用吗?
  • @TomKarzes 哦,哇!我以为我是在返回标记一个布尔值(真或假),所以它会打印真或假。如果 x y 那么 flag == false。 mmh 对于 printf,是否是 printf(" %s \n", twoStrComp(x,y)); ?
  • 您希望在终端中看到什么?为什么您认为终端上没有打印任何内容?您的程序中是否有地方打印比较函数的结果?
  • 查看对twoStrComp的调用。您正在将返回值放在地板上。 what twoStrComp 返回并不重要,因为你忽略了它。
  • 比较一个字符串的 2 个字符可以产生 3 种不同的结果:小于、等于、大于。你最后的else 部分永远不会到达。相反,如果您找到前 2 个匹配字符,您将返回 0。

标签: c string string-comparison


【解决方案1】:

所以我试图比较 2 个字符串,如果第一个 (x) 小于第二个,则 twoStrComp 方法应返回 true,否则应为 false。

我建议为您的函数取一个更好的名称。它不是通用的比较函数,但它更像是一个isSmaller 函数。您不关心相同或更大值的不同情况。

这是我的代码,虽然当我尝试在终端上运行它时没有任何反应

如果您想在控制台上看到任何内容,则需要打印一些内容。为此,您可以使用printf

我还想知道使用指针是否会更有效,但我不确定如何声明它们,我可以做 twoStrComp(*x,*y) 吗?

如果要传递指针,可以这样声明:

bool twoStrComp(const char *x, const char *y)

但是.... 将数组作为参数传递会导致传递指针。数组在函数的参数列表中使用时会衰减为指针。您不会看到任何性能提升。

关于您的代码... 我指的是在编辑历史中列为版本 2 的版本。

你返回 1 或 -1。当您使用类型bool 作为返回类型时,您应该考虑使用TRUEFALSE。或者在某些情况下至少返回 0。

设置flag=0; 没有任何意义。如果之前不是 0,你就会离开循环。

您不检查是否在字符串末尾进行比较。

修复这些问题并包含一些测试用例的版本可能如下所示:

#include <stdbool.h>
#include <stdio.h>

bool twoStrComp(char string1[], char string2[]);

int main(void)
{
  char x[]= "test";
  char y[]= "another";
  bool isSmaller = twoStrComp(x,y);
  printf("Is x(%s) smaller than y(%s)? %s\n", x, y, isSmaller ? "YES" : "NO");

  isSmaller = twoStrComp(y,x);
  printf("Is y(%s) smaller than x(%s)? %s\n", y, x, isSmaller ? "YES" : "NO");

  char x2[]= "aaa";
  char y2[]= "aab";

  isSmaller = twoStrComp(x2,y2);
  printf("Is x2(%s) smaller than y2(%s)? %s\n", x2, y2, isSmaller ? "YES" : "NO");

  isSmaller = twoStrComp(y2,x2);
  printf("Is y2(%s) smaller than x2(%s)? %s\n", y2, x2, isSmaller ? "YES" : "NO");

  isSmaller = twoStrComp(x2,x2);
  printf("Is x2(%s) smaller than x2(%s)? %s\n", x2, x2, isSmaller ? "YES" : "NO");
}

bool twoStrComp(char string1[], char string2[])
{
  int i=0;
  while (true)
  {
    if (string1[i] < string2[i])
      return true; // First one smaller than second one...
    else if (string1[i] > string2[i])
      return false; // First one larger than second one...
    else if (!string1[i])
      return false; // identical and end of strings reached.
    else
      i++;   // identical, not yet decided.
  }

  // We cannot get here, but the compiler complains...
  return false;         
}  

【讨论】:

    猜你喜欢
    • 2022-01-02
    • 2013-12-16
    • 2012-04-29
    • 2013-10-22
    • 2018-05-17
    • 2010-12-08
    • 2019-11-04
    • 1970-01-01
    相关资源
    最近更新 更多