C bool 函数按字母顺序比较 2 个字符串

标签 c string string-comparison

所以我尝试比较 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;
}

最佳答案

So I am trying to compare 2 strings and if the first one (x) is smaller than the second one the method twoStrComp should return true, else it should be false.

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

This is my code, although when I try to run it on my terminal nothing comes up

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

Also I was wondering if it would be more efficient with pointers but I was unsure of how to declare them, can I do 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;         
}  

关于C bool 函数按字母顺序比较 2 个字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47613406/

相关文章:

c - 如何部分比较 C 中的两个字符串?

string-comparison - 如何简单地检查两个词的接近度?

c - 递归函数中返回的局部变量

c - 在 C 中通过梯形规则积分

c - C 中的是/否循环

javascript - 如何将二维数组中的内容拆分为另一个二维 Javascript

string - 坚持 Apostolico-Crochemore 算法

android - 为 Android 编译和运行 C 代码

c# - 每个 ComboBox.Selected... 返回什么?

python-3.x - For/if 状态以一种方式工作,但不以另一种方式工作