【问题标题】:How to check if a byte from a byte array contains 0 using C?如何使用C检查字节数组中的字节是否包含0?
【发布时间】:2021-01-27 10:15:29
【问题描述】:

我有一个 byte array 或 strings。如何检查该数组中的字节是否包含 0?例如:

char byte_arr[5] = "abc00";

上述数组的最后两个字节包含 0。如何在c 中检测到?

【问题讨论】:

  • 您没有字符串的字节数组。您的代码无效。
  • @Cheatah 在这种情况下你能纠正我吗?
  • char byte_arr[5] = "abc00"; 将是有效的。
  • 将数组的元素与您要查找的值进行比较有什么问题?
  • 如何检查这个数组是否包含零值? int arr [5] = {1,2,0,3,4};。为此编写代码,然后尝试对 char 数组执行相同操作。

标签: arrays c string


【解决方案1】:

遵循的简单算法

count_zero = 0
for each element a, in byte_array
    check if a is '0'
        count_zero++  

【讨论】:

  • 您也可以使用 ASCII 值 48 而不是 '0' 进行比较。 为什么?!?! '0' 可以在任何地方使用.如果您硬编码48,则代码不必要地限制为 ASCII。
  • @AndrewHenle 没有考虑这一点,已修复。
【解决方案2】:

即使我们编译这段代码(假设uint8 被定义在某处)

uint8 byte_arr[5] = "abc00";

那么这个数组中的任何字节(除了第 5 个)的值都为零。字符'0' 的最后两个保持值取决于所使用的字符编码。

要检查数组的特定元素是否包含某个值,您需要将其与该值进行比较

if(byte_arr[4] == '0') printf("Success!!!!\n");
else printf("Failure!!!!\n");

【讨论】:

    【解决方案3】:

    对于初学者,您的字符数组不包含字符串。

    char byte_arr[5] = "abc00";
    

    因此,要确定字符“0”是否存在于数组中,您必须使用其大小遍历它的元素。

    在这种情况下,您可以使用循环,例如 while 循环

    size_t i = 0;
    char c = '0';
    
    while ( i < 5 && byte_arr[i] != c ) i++;
    
    if ( i != 5 ) printf( "The character %c is present at position %zu\n, c, i );
    

    如果您需要确定包含字符 '0' 的所有元素的位置或对其进行计数,则可以使用例如 for 循环。

    #include <stdio.h>
    
    int main(void) 
    {
        enum { N = 5 };
        char byte_arr[N] = "abc00";
        char c = '0';
        
        size_t count = 0;
        
        for ( size_t i = 0; i < N; i++ )
        {
            if ( byte_arr[i] == c )
            {
                ++count;
                printf( "At the position %zu there is the character '%c'\n", i, c );
            }
        }
        
        printf( "There are %zu elements in the array that contains '%c'.\n", count, c );
        
        return 0;
    }
    

    程序输出是

    At the position 3 there is the character '0'
    At the position 4 there is the character '0'
    There are 2 elements in the array that contains '0'.
    

    如果您要声明数组,例如

    char byte_arr[] = "abc00";
    

    那么数组将包含一个字符串。在这种情况下,您可以使用标准字符串函数strchr 检查字符串中是否存在给定字符。例如

    #include <string.h>
    
    //...
    
    char byte_arr[] = "abc00";
    char c = '0';
    
    char *p = strchr( byte_arr, c );
    
    if ( p != NULL ) printf( "The character %c is present at position %zu\n, c, ( size_t )( p - byte_arr ) );
     
    

    【讨论】:

      【解决方案4】:

      有一个标准函数,在string.h,如果任务只是确定是否有:

      if ( memchr(byte_arr, '0', sizeof byte_arr) )
          printf("There was a zero.\n");
      

      【讨论】:

        【解决方案5】:

        您可以循环数组直到到达末尾或找到所需的字符:

        int main(void) {
            char byte_arr[5] = "abc00";
            for (int i =0; i<5; ++i)
            {
                if(byte_arr[i]=='0'){
                    printf("Found!!");
                    break;
                }
            }
            return 0;
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2019-08-13
          • 2013-09-30
          • 1970-01-01
          • 2016-05-02
          • 2010-11-14
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多