zoukankan      html  css  js  c++  java
  • c语言之利用指针复制字符串的几种形式

    第一种:

    #include<stdio.h>
    #include<iostream>
    
    void copy_string(char* p1, char* p2) {
        for (; *p1 != ''; *p1++,*p2++)
        {
            *p2 = *p1;
        }
        *p2 = '';
    }
    
    int main() {
        char* str1 = (char*) "hello world";
        char str2[] = "i am a student";
        copy_string(str1, str2);
        printf("%s
    ",str2);
        system("pause");
        return 0;
    }

    第二种:

    #include<stdio.h>
    #include<iostream>
    
    void copy_string(char* p1, char* p2) {
        while ((*p2 = *p1) != '')
        {
            *p2++;
            *p1++;
        }
    }
    
    int main() {
        char* str1 = (char*)"hello world";
        char str2[] = "i am a student";
        copy_string(str1, str2);
        printf("%s
    ", str2);
        system("pause");
        return 0;
    }

    第三种:

    #include<stdio.h>
    #include<iostream>
    
    void copy_string(char* p1, char* p2) {
        //指针运算符比++优先级高
        //也就是先将*p1的值给*p2,再进行++操作,i++是先赋值,后自增
        while ((*p2++ = *p1++) != '')
    }
    
    int main() {
        char* str1 = (char*)"hello world";
        char str2[] = "i am a student";
        copy_string(str1, str2);
        printf("%s
    ", str2);
        system("pause");
        return 0;
    }

    第四种:

    #include<stdio.h>
    #include<iostream>
    
    void copy_string(char* p1, char* p2) {
        while (*p1 != '') {
            *p2++ = *p1++;
        }
        *p2 = '';
    }
    
    int main() {
        char* str1 = (char*)"hello world";
        char str2[] = "i am a student";
        copy_string(str1, str2);
        printf("%s
    ", str2);
        system("pause");
        return 0;
    }

    第五种:

    #include<stdio.h>
    #include<iostream>
    
    void copy_string(char* p1, char* p2) {
        //当*p2++ = *p1++变为0时,就会结束循环
        while (*p2++ = *p1++) {
            ; //'' == 0;结束标志
        }
    }
    
    int main() {
        char* str1 = (char*)"hello world";
        char str2[] = "i am a student";
        copy_string(str1, str2);
        printf("%s
    ", str2);
        system("pause");
        return 0;
    }

    第六种:

    #include<stdio.h>
    #include<iostream>
    
    void copy_string(char* p1, char* p2) {
        for (; *p2++ = *p1++;) {
            ; //'' == 0;结束标志
        }
    }
    
    int main() {
        char* str1 = (char*)"hello world";
        char str2[] = "i am a student";
        copy_string(str1, str2);
        printf("%s
    ", str2);
        system("pause");
        return 0;
    }

    第七种:

    #include<stdio.h>
    #include<iostream>
    
    void copy_string(char str1[], char str2[]) {
        char* p1, * p2;
        p1 = str1;
        p2 = str2;
        while((*p2++ = *p1++)!='') {
            ; //'' == 0;结束标志
        }
    }
    
    int main() {
        char* str1 = (char*)"hello world";
        char str2[] = "i am a student";
        copy_string(str1, str2);
        printf("%s
    ", str2);
        system("pause");
        return 0;
    }
  • 相关阅读:
    排序操作
    逻辑回归
    二叉树的建立以及相关操作,平衡二叉树
    【AMAD】cookiecutter-django -- 是一个构建Django项目的脚手架工具
    【AMAD】django-allauth
    【AMAD】django-formapi -- 一个DJANGO API框架,可使用签名request,可使用form作为API的验证工具
    【AMAD】django-cities -- 为Django项目提供国家,城市数据
    【AMAD】django-countries -- 为Django app的form提供country选项,为model提供CountryField
    【AMAD】django-social-auth -- 让django使用社交网络oauth鉴权变得极为轻松!
    每周分享第9期(2019.6.1)
  • 原文地址:https://www.cnblogs.com/xiximayou/p/12121521.html
Copyright © 2011-2022 走看看