计算字符数组中的1的数量

cou*_*mer 3 c arrays comparison character

我试图用递归类型程序计算表示二进制数的字符数组中的1的数量.但是,似乎我的程序只是计算数组中的字符数.我不知道我是否只是在比较错误与否,但我似乎无法找到问题

#include <stdio.h>
# include <stdlib.h>
#include <string.h>
#define SIZE 20

int determine (char array[SIZE], int count, int track );

int main()
{ 
int answer = 0; 
char input[SIZE]={"1001001"};
int count = 0;
int track = 0;

answer = determine(input, count, track);

 printf("The number of 1's is %d ", answer);

system("PAUSE");
return 0;
 }

 int determine(char array[], int count, int track)
{   

 if (array[track] != '\0')
 {
    if ( array[track] == '1');
    {
         count++;
    }
    return determine(array, count, track = track+1);    
 }
 else 
 {
      return count;
 }
}
Run Code Online (Sandbox Code Playgroud)

rak*_*rul 11

在方法中determine():

if ( array[track] == '1');
Run Code Online (Sandbox Code Playgroud)

删除分号;.分号使if条件执行empty块.因此,count++无论if条件是成功(true)还是不成功(),都将始终执行false.

我用你的代码运行;并得到输出:

1的数量是7

没有;:

1的数量是3