LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 689|回复: 1

小问题

[复制链接]
发表于 2009-6-9 11:53:51 | 显示全部楼层 |阅读模式
一个小问题:
一个由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
}
发表于 2009-6-9 16:49:40 | 显示全部楼层
如果是‘0’-‘1’=?
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表