|
一个小问题:
一个由ASCII码组成的字串,
计算其中数字‘1’ 的个数。
下面的程序是在当字串是hex的基础上修改的,但好象不可以成功的。
(系统把空格全都删除了,没有了缩进,不好看了,请见谅。)
#include <stdio.h>
#include <string.h>
int main(void)
{
char inputstr[]="101010101010101"; // it's ascii
int i=0;
int count=0;
int len=0;
char newstr[16];
newstr[0]='\0';
len=strlen(inputstr);
if(len == 0)
{
printf("[Debug] It's Error! \n");
return 1;
}
/*
//i find the follow is invalid!
for(i=0;i<len;i++)
{
newstr = inputstr - '1';
}
newstr[len] ='\0';
*/
for(i=0;i<len;i++)
{
if(newstr & 0x01 == 1)
count++;
}
printf("the number of binary 1 in the string is %d \n", count);
return 0; //use gcc
} |
|