c字符串复制、查找、反转

1、字符串复制

void  copy_str1(char  *from,  char  *to) 
{ 
    for  (;  *from!='\0';  from++,  to++) 
    { 
        *to  =  *from; 
    } 

    *to  =  '\0'; 
    return  ; 
} 
void  copy_str2(char  *from,  char  *to) 
{ 
    for  (;  *from!='\0';) 
    { 
        *to++  =  *from++;   //   先  *to  =  *from;   再from++,  to++  
    } 
    *to  =  '\0';  // 
    return  ; 
} 
void  copy_str3(char  *from,  char  *to) 
{ 
    while(  (*to  =  *from)  !=  '\0'  ) 
    { 
        from  ++; 
        to  ++; 
    } 
} 
void  copy_str4(char  *from  ,  char  *to) 
{ 
    while  (  (*to++  =  *from++)  !=  '\0') 
    { 
        ; 
    } 
} 
void  copy_str5(char  *from  ,  char  *to) 
{ 
    //*(0)  =  'a'; 
    while  (  (*to++  =  *from++)  ) 
    { 
        ; 
    } 
} 
void  copy_str5_err(char  *from  ,  char  *to) 
{ 
    //*(0)  =  'a'; 
    while  (  (*to++  =  *from++)  ) 
    { 
        ; 
    } 
    printf("from:%s  \n",  from); 
} 
//不要轻易改变形参的值,  要引入一个辅助的指针变量.  把形参给接过来..... 
int  copy_str6(char  *from  ,  char  *to) 
{ 
    //*(0)  =  'a'; 
    char  *tmpfrom  =  from; 
    char  *tmpto  =  to; 
    if  (  from  ==  NULL  ||  to  ==  NULL) 
    { 
        return  -­‐1; 
    } 
    while  (*tmpto++  =  *tmpfrom++)  ;   //空语句 
    printf("from:%s  \n",  from); 
    return  0; 
} 

2、字符串查找

int len(char *p)
{
	  int  ncount  =  0; 
	    //初始化  让p指针达到查找的条件 
	    char  *p  =  "2abcd3333322qqqabcd"; 
	    while  (  (p  =  strstr(p,  "abcd"))  !=  NULL  ) 
	    { 
	        ncount  ++; 
	        p  =  p  +  strlen("abcd");  //让p指针达到查找的条件 
	        if  (*p  ==  '\0') 
	        { 
	            break; 
	        } 
	    } 
	    return ncount;
    }

3、字符串反转

char*  inverse(char  *str) 
{ 
    int  length  =  strlen(str); 
    char  *p1  =  NULL; 
    char  *p2  =  NULL; 
    char  tmp_ch; 
    if  (str  ==  NULL)  { 
        return  NULL; 
    } 
    p1  =  str; 
    p2  =  str  +  length  -­‐1; 
    while  (p1  <  p2)  { 
        tmp_ch  =  *p1; 
        *p1  =  *p2; 
        *p2  =  tmp_ch; 
        ++p1; 
        -­‐-­‐p2; 
    } 
    return  str; 
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值