现在的位置: 首页 > 综合 > 正文

字符串逆序和统计数据中有多少位是1

2012年08月26日 ⁄ 综合 ⁄ 共 938字 ⁄ 字号 评论关闭

void invert(char *str,int len)
{
 int i,j,n,temp;
 n=(len-1)/2;
 
 for(i=0;i<=n;i++)
 {
  j=len-1-i;
  
  temp=*(str+i);
  *(str+i)=*(str+j);
  *(str+j)=temp;
 }
 
}

int count(char *s,int len)
{
 int i,j,cnt=0;
 for(j=0;j<len;j++)
 {
  for(i=0;i<8;i++)
  {
   if((*s&1)==1)
   {
    cnt++;
   }
   *s=*s>>1;
  }
  s++;
 }
 printf("cnt=%d /n",cnt);
 return cnt;
}

int main()
{
 char array[]="happy life";
 char b[10]={3,3,3,11,1,1,1,1,1,1};
 int  countvalue=0;
 
 countvalue=count(b,sizeof(b));
 invert(array,sizeof(array));

}

void invert(char *str,int len)
{
 int i,j,n,temp;
 n=(len-1)/2;
 
 for(i=0;i<=n;i++)
 {
  j=len-1-i;
  
  temp=*(str+i);
  *(str+i)=*(str+j);
  *(str+j)=temp;
 }
 
}

int count(char *s,int len)
{
 int i,j,cnt=0;
 for(j=0;j<len;j++)
 {
  for(i=0;i<8;i++)
  {
   if((*s&1)==1)
   {
    cnt++;
   }
   *s=*s>>1;
  }
  s++;
 }
 printf("cnt=%d /n",cnt);
 return cnt;
}

int main()
{
 char array[]="happy life";
 char b[10]={3,3,3,11,1,1,1,1,1,1};
 int  countvalue=0;
 
 countvalue=count(b,sizeof(b));
 invert(array,sizeof(array));

}

抱歉!评论已关闭.