在C中的字符串数组中查找'1'的计数

问题描述 投票:-1回答:1

我正在做一些作业,我需要用C编写一个程序,该程序计算字符串数组中'1'的次数。例如,我有一个字符串数组,其中有3个字符串:D12,B11和F1。

程序需要计算所有的'1'。对于此示例为4。我该如何实现?

这里是我到目前为止的代码:

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

int main()
{
    int i=0,N,br=0;
    char s[10][20];

    scanf("%d", &N);
    for(i=0;i<N;i++)
    {
       scanf("%s", &s[i]);
       if(strchr(s[i], '1') != NULL);
       {
           ++br;
       }
    }
    printf("%d", br);
}
c
1个回答
0
投票
if(strchr(s[i], '1') != NULL); { ++br; }

用于以下循环

   for ( const char *p = s[i]; ( p = strchr( p, '1' ) ) != NULL; ++p )
   {
       ++br;
   }

请注意此呼叫

scanf("%s", &s[i]);

至少必须像]进行更改>

scanf("%s", s[i]);
© www.soinside.com 2019 - 2024. All rights reserved.