对字符串的操作

 1、将一个字符串逆序

char *Reverse(char *s)
{
    // p指向字符串头部
    char *p = s ;
    // q指向字符串尾部
    char *q = s ;
    while(*q)
        ++q ;
    q -- ;
    // 交换并移动指针,直到p和q交叉
    while(q > p)
    {
        char t = *p ;
        *p++ = *q ;
        *q-- = t ;
    }
    return s ;

2、计算一个字节里(byte)里面有多少bit被置1 

 int Bit1Count(int Num)  
{int Count_=0;
 while(Num)
 {
 Count+=(Num&1);
 Num=Num>>1;
 }
 return Count;
}
             
  
//计算整数num换算成2进制后bit 为1的个数
int bitCount(int num)
{
  int count = 0;  // 初始化计数为0
  do {
      count += num & 0x1;  // 每次num的最低位若是1,则count++
     } while((num >> 1) != 0);  //每次循环将num右移一位,直至所有bit为1的均被移走(数完) 
  return count;

3、将一个链表逆序

4、搜索给定的字节(byte) 5、在一个字符串中找到可能的最长的子字符串 6、整数转换为字符串

int atoi(char const *s) //"123"  '1' 1
{
 int num = 0;
 //sizeof(s) / sizeof(char);
 
 while (*s)
 {
  num = 10 *num + *s-'0';
  printf("num=%d\n",num);
   s++;
 }
 return num;
}


7、字符串转换为整数

char *itoa(int data, char *s) //char *s = str
{
 //char a[N];
 int i = 0, j = 0;
 char t;
 while (data)
 {
  s[i++] = data % 10 + '0'; //3 '3'
  data /= 10;
 }
 s[i] = '\0'; //"123"
 i--;

 while (j < i)
 {
  t = s[i];
  s[i] = s[j];
  s[j] = t;
  j++;
  i--;
 }

 return s;
}


/*
* 题目:将一个字符串逆序
* 完成时间:2006.9.30深圳极讯网吧
* 版权归刘志强所有
* 描述:写本程序的目的是希望练一下手,希望下午去面试能成功,不希望国庆节之后再去找工作拉!
*/
#include
using namespace std;
//#define NULL ((void *)0)
char * mystrrev(char * const dest,const char * const src)
{
if (dest==NULL && src==NULL)
   return NULL;
char *addr = dest;
int val_len = strlen(src);
dest[val_len] = '\0';
int i;
for (i=0; inext;
while(q!=NULL)
{
temp=q->next;
q->next=p;
p=q;
q=temp;
}
这样增加个辅助的指针就行乐。
ok 通过编译的代码:
#include  
#include  
#include  
typedef struct List{
int data;
struct List *next;
}List;
List *list_create(void)
{
struct List *head,*tail,*p;
int e;
head=(List *)malloc(sizeof(List));
tail=head;
printf("\nList Create,input numbers(end of 0):");
scanf("%d",&e);
while(e){
p=(List *)malloc(sizeof(List));
p->data=e;
tail->next=p;
tail=p;
scanf("%d",&e);}
tail->next=NULL;
return head;
}
List *list_reverse(List *head)
{
List *p,*q,*r;
p=head;
q=p->next;
while(q!=NULL)
{
r=q->next;
q->next=p;
p=q;
q=r;
}
head->next=NULL;
head=p;
return head;
}
void main(void)
{
struct List *head,*p;
int d;
head=list_create();
printf("\n");
for(p=head->next;p;p=p->next)
printf("--%d--",p->data);
head=list_reverse(head);
printf("\n");
for(p=head;p->next;p=p->next)
printf("--%d--",p->data);
}
       编写函数数N个BYTE的数据中有多少位是1。
解:此题按步骤解:先定位到某一个BYTE数据;再计算其中有多少个1。叠加得解。
#incluede
#define N 10
//定义BYTE类型别名
#ifndef BYTE
typedef unsigned char BYTE;
#endif
int comb(BYTE b[],int n)
{
int count=0;
int bi,bj;
BYTE cc=1,tt;
//历遍到第bi个BYTE数据
for(bi=0;bi>1;
tt=tt/2;
}
}
return count;
}
//测试
int main()
{
BYTE b[10]={3,3,3,11,1,1,1,1,1,1};
cout
1。编写一个 C 函数,该函数在一个字符串中找到可能的最长的子字符串,且该字符串是由同一字符组成的。
char * search(char *cpSource, char ch)
{
char *cpTemp=NULL, *cpDest=NULL;
int iTemp, iCount=0;
while(*cpSource)
{
if(*cpSource == ch)
{
iTemp = 0;
cpTemp = cpSource;
while(*cpSource == ch)
++iTemp, ++cpSource;
if(iTemp > iCount)
iCount = iTemp, cpDest = cpTemp;
if(!*cpSource)
break;
}
++cpSource;
}
return cpDest;
}
#include
#include
//
// 自定义函数MyAtoI
// 实现整数字符串转换为证书输出
// 程序不检查字符串的正确性,请用户在调用前检查
//
int MyAtoI(char str[])
{
int i;
int weight = 1; // 权重
int rtn = 0; // 用作返回
for(i = strlen(str) - 1; i >= 0; i--)
{
   rtn += (str
- '0')* weight; //
   weight *= 10; //
增重
}
return rtn;
}
void main()
{
char str[32];
printf("Input a string :");
gets(str);
printf("%d\n", MyAtoI(str));
}
#include
#include
void reverse(char s[])
{   //字符串反转
    int c, i=0, j;
    for(j=strlen(s)-1;i0);
    //如果是负数,补上负号
    if(sign

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值