zoukankan      html  css  js  c++  java
  • 项目开发字符串模型strstr_while

    #define  _CRT_SECURE_NO_WARNINGS 
    #include <stdlib.h>
    #include <string.h>
    #include <stdio.h>
    
    void main71()
    {
        char a[] = "i am a student";
        char b[64];
        int  i = 0;
    
        for (i=0; *(a+i) != ''; i++)
        {
            *(b+i) = *(a+i);
        }
    
        //0没有copy到b的buf中.
    
        b[i] = ''; //重要
        printf("a:%s 
    ", a);
        printf("b:%s 
    ", b);
    
        system("pause");
        return ;
    }
    
    //字符串copy函数技术推演
    
    //字符串copy函数
    
    //form形参 形参to 的值 不停的在变化....
    //不断的修改了from和to的指向
    void copy_str21(char *from, char *to)
    {
        for (; *from!=''; from++, to++)
        {
             *to = *from;
        }
        *to = '';
    
        return ;
    }
    
    //*操作 和++的操作
    //++ 优先级高 
    void copy_str22(char *from, char *to)
    {
        for (; *from!='';)
        {
            *to++ = *from++;  //  先 *to = *from;  再from++, to++ 
        }
        *to = ''; //
    
        return ;
    }
    
    void copy_str23(char *from, char *to)
    {
        while( (*to = *from) != '' )
        {
            from ++; 
            to ++;
        }
    }
    
    void copy_str24(char *from , char *to)
    {
        while ( (*to++ = *from++) != '')
        {
            ;
        }
    }
    
    
    void copy_str25(char *from , char *to)
    {
        //*(0) = 'a';
        while ( (*to++ = *from++) )
        {
            ;
        }
    }
    
    
    void copy_str25_err(char *from , char *to)
    {
        //*(0) = 'a';
        while ( (*to++ = *from++) )
        {
            ;
        }
    
        printf("from:%s 
    ", from);
        
    }
    
    
    //不要轻易改变形参的值, 要引入一个辅助的指针变量. 把形参给接过来.....
    int copy_str26_good(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 
    ", from);
            
    }
    
    int main111()
    {
        int ret = 0;
        char *from = "abcd";
        char buf2[100]; 
        //copy_str21(from, buf2);
        //copy_str22(from,buf2);
        //copy_str23(from, buf2);
        //copy_str24(from, buf2);
        //copy_str25(from ,buf2);
        //printf("buf2:%s 
    ", buf2);
    
        {
            //错误案例
            char *myto = NULL;  //要分配内存
            //copy_str25(from,myto );
        }
        {
            char *myto = NULL;  //要分配内存
            
            ret = copy_str26_good(from, myto);
            if (ret != 0)
            {
                printf("func copy_str26_good() err:%d  ", ret);
                return ret;
            }
        }
        system("pause");
        return ret;
    }
    
    
    int main777()
    {
        int ret = 0;
        char *from = "abcd";
        char buf2[100]; 
    
        printf("copy_str25_err begin
    ");
        copy_str25_err(from, buf2);
        copy_str26_good(from, buf2);
        printf("copy_str25_err end
    ");
        return 0;
    }

     

  • 相关阅读:
    自主开发与带兵打仗
    外包项目的内外部管理
    服务器运维工程师岗位要求
    “互联网+”下, 经销商价值再思考
    外包软件项目管理要抓住关键点
    软件外包项目管理的经验感悟
    关于软件外包项目管理的想法
    九型人格判定
    好的学习材料
    前端学习的好去处
  • 原文地址:https://www.cnblogs.com/yaozhenhua/p/9416461.html
Copyright © 2011-2022 走看看