C - 比较数字字符串

发布于 2024-11-16 00:14:55 字数 439 浏览 6 评论 0原文

出于专业的好奇心,在 C 中比较两个全数字字符串的最安全/最快/最有效的方法是什么?

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(void){

char str1[5] = "123";
char str2[5] = "123";
char *ptr;

if(atoi(str1) == atoi(str2))
    printf("Equal strings");

if(strtol(str1,&ptr,10) == strtol(str2,&ptr,10))
    printf("Equal strings");

if(strcmp(str1,str2)==0)
    printf("Equal strings");

return 0;
}

Out of professional curiosity, what is the safest / fastest / most efficient way to compare two fully numeric strings in C?

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(void){

char str1[5] = "123";
char str2[5] = "123";
char *ptr;

if(atoi(str1) == atoi(str2))
    printf("Equal strings");

if(strtol(str1,&ptr,10) == strtol(str2,&ptr,10))
    printf("Equal strings");

if(strcmp(str1,str2)==0)
    printf("Equal strings");

return 0;
}

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

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

发布评论

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

评论(6

月依秋水 2024-11-23 00:14:55

strcmp () 在我看来,因为它不需要任何数字转换。但在这种情况下,您需要确保其中之一存储仅包含数字字符的字符串。

您还可以对字符串

EDIT1

执行 memcmp ()正如其他人关于前导零所指出的那样,您可以手动扫描前导零并调用 strcmp ( )memcmp () 通过将指针传递给第一个非零数字。

EDIT2

下面的代码告诉了我想说的。这仅适用于整数,不适用于浮点数。

int main (void) {
  char s1[128], s2[128];
  char *p1 = s1, *p2 = s2;

  /* populate s1, s2 */

  while (*p1 && (*p1 == '0'))
    p1++;

  while (*p2 && (*p2 == '0'))
    p2++;

  if (strcmp (p1, p2) == 0)
    printf ("\nEqual");
  else
    printf ("\nNot equal");

  printf ("\n");
  return 0;
}

对于浮点数,应手动删除小数点后的尾随零。

或者手动完成所有工作。

EDIT4

我还希望您看看这段浮点代码。这将检测小数点之前的前导零和小数点之后的尾随零。例如,对于

以下代码,00000000000001.100000000000001.1 将是 Equal

int main (void) {
  char s1[128], s2[128];
  char *p1, *p2, *p1b, *p2b;

  printf ("\nEnter 1: ");
  scanf ("%s", s1);
  printf ("\nEnter 2: ");
  scanf ("%s", s2);

  p1 = s1;
  p2 = s2;
  /* used for counting backwards to trim trailing zeros
   * in case of floating point
   */
  p1b = s1 + strlen (s1) - 1;
  p2b = s2 + strlen (s2) - 1;


  /* Eliminate Leading Zeros */
  while (*p1 && (*p1 == '0'))
    p1++;

  while (*p2 && (*p2 == '0'))
    p2++;

  /* Match upto decimal point */
  while (((*p1 && *p2) && ((*p1 != '.') && (*p2 != '.'))) && (*p1 == *p2))
  {
    p1++;
    p2++;
  }

  /* if a decimal point was found, then eliminate trailing zeros */
  if ((*p1 == '.') && (*p2 == '.'))
  {
    /* Eliminate trailing zeros (from back) */
    while (*p1b == '0')
      p1b--;
    while (*p2b == '0')
      p2b--;

    /* match string forward, only upto the remaining portion after
     * discarding of the trailing zero after decimal
     */
    while (((p1 != p1b) && (p2 != p2b)) && (*p1 == *p2))
    {
      p1++;
      p2++;
    }
  }

  /* First condition on the LHS of || will be true for decimal portion
   * for float the RHS will be . If not equal then none will be equal
   */
  if (((*p1 == '\0') && (*p2 == '\0')) ||  ((p1 == p1b) && (p2 == p2b)))
    printf ("\nEqual");
  else
    printf ("\nNot equal");

  printf ("\n");
  return 0;
}

使用前需要进行一些测试。

strcmp () in my opinion, as it does not need any numeric conversions. But in this case you need to make sure that one of them stores a string which contains only numeric characters.

Also you can do memcmp () on the string

EDIT1

As pointed out by others about the leading zeros, you can manually scan through the leading zeros and call strcmp () or memcmp () by passing a pointer to the first non-zero digit.

EDIT2

The below code tells what i am trying to say. This is only for integers, not for floating point numbers.

int main (void) {
  char s1[128], s2[128];
  char *p1 = s1, *p2 = s2;

  /* populate s1, s2 */

  while (*p1 && (*p1 == '0'))
    p1++;

  while (*p2 && (*p2 == '0'))
    p2++;

  if (strcmp (p1, p2) == 0)
    printf ("\nEqual");
  else
    printf ("\nNot equal");

  printf ("\n");
  return 0;
}

For floating point numbers, the trailing zeros after the decimal point should be chopped out manually.

Or do the whole stuff manually.

EDIT4

I would also like you to have a look at this code for floating point. This will detect leading zeros before the decimal and trailing zeros after the decimal. For example

00000000000001.10000000000000 and 1.1 will be Equal for the below code

int main (void) {
  char s1[128], s2[128];
  char *p1, *p2, *p1b, *p2b;

  printf ("\nEnter 1: ");
  scanf ("%s", s1);
  printf ("\nEnter 2: ");
  scanf ("%s", s2);

  p1 = s1;
  p2 = s2;
  /* used for counting backwards to trim trailing zeros
   * in case of floating point
   */
  p1b = s1 + strlen (s1) - 1;
  p2b = s2 + strlen (s2) - 1;


  /* Eliminate Leading Zeros */
  while (*p1 && (*p1 == '0'))
    p1++;

  while (*p2 && (*p2 == '0'))
    p2++;

  /* Match upto decimal point */
  while (((*p1 && *p2) && ((*p1 != '.') && (*p2 != '.'))) && (*p1 == *p2))
  {
    p1++;
    p2++;
  }

  /* if a decimal point was found, then eliminate trailing zeros */
  if ((*p1 == '.') && (*p2 == '.'))
  {
    /* Eliminate trailing zeros (from back) */
    while (*p1b == '0')
      p1b--;
    while (*p2b == '0')
      p2b--;

    /* match string forward, only upto the remaining portion after
     * discarding of the trailing zero after decimal
     */
    while (((p1 != p1b) && (p2 != p2b)) && (*p1 == *p2))
    {
      p1++;
      p2++;
    }
  }

  /* First condition on the LHS of || will be true for decimal portion
   * for float the RHS will be . If not equal then none will be equal
   */
  if (((*p1 == '\0') && (*p2 == '\0')) ||  ((p1 == p1b) && (p2 == p2b)))
    printf ("\nEqual");
  else
    printf ("\nNot equal");

  printf ("\n");
  return 0;
}

Needs some testing before use.

黑寡妇 2024-11-23 00:14:55

str(n)cmp 是最快、最安全的。

str(n)cmp is the fastest and safest.

来世叙缘 2024-11-23 00:14:55

假设您希望它们相同,strncmp 将是最快、最安全的,因为它可以进行直接比较,无需任何转换。它通常也被认为比 strcmp 更安全。

但是,如果您希望 000 相等,或者以其他方式稍微不同地表示相同的数字,则需要使用 atoi.

Assuming you are looking for them to be idential, strncmp will be the fastest and safest since it can do a direct comparison without any conversions. It is also generally considered safer than strcmp.

However, if you want 00 and 0 to be equal, or other ways that you can represent the same number slightly differently, you will need to use atoi.

莫相离 2024-11-23 00:14:55

在我看来,“最安全”的方法可能是将两个参数都转换为整数然后进行测试,这样您就可以避免潜在的前导零问题。不过,这可能不是最快或最有效的方法。

In my opinion, the "safest" way would likely be to convert both arguments to integers and then test, as that way you'll avoid the potential leading-zeros problem. It's probably not the fastest or most efficient method, though.

滥情稳全场 2024-11-23 00:14:55

您可以简单地使用以下内容:

if(strcmp("123","123") == 0)

{

  printf("The strings are equal");

}

else

{

  printf("The strings are not equal.");

}

在我看来它应该有效。

You can simply use following :

if(strcmp("123","123") == 0)

{

  printf("The strings are equal");

}

else

{

  printf("The strings are not equal.");

}

In my opinion it should work.

深海少女心 2024-11-23 00:14:55

我建议对整数采用这种方式:

int strcmp_for_integers(char *aa, char *bb){
    char aa2[11] = "";
    char bb2[11] = "";
    int answer;

    sprintf(aa2, "%010d", atoi(aa));
    sprintf(bb2, "%010d", atoi(bb));
    answer = strcmp(aa2, bb2);

    return answer;
}

I suggest this way for integers:

int strcmp_for_integers(char *aa, char *bb){
    char aa2[11] = "";
    char bb2[11] = "";
    int answer;

    sprintf(aa2, "%010d", atoi(aa));
    sprintf(bb2, "%010d", atoi(bb));
    answer = strcmp(aa2, bb2);

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