zoukankan      html  css  js  c++  java
  • C语言:通过指针对字符串进行拼接

    //

    //  main.c

    //  Pointer_stringcat

    //

    //  Created by ma c on 15/8/2.

    //  Copyright (c) 2015年 bjsxt. All rights reserved.

    //  要求:使用指针连接字符串,并将连接后的字符串输出到屏幕上。

     

    #include <stdio.h>

    #include<string.h>

    void Pointer_stringcat(char *str1,const char *str2)

    {

        while (*(str1++)!='');     //一直将指向str1的指针移到字符串的末尾

        str1--;

        while (*str2!='')

        {

            *(str1++) = *(str2++);  //开始连接

        }

        *str1 = '';               //连接完后,添加上字符串结束标识符

    }

    int main(int argc, const char * argv[])

    {

        char s1[] = "hello ";     //这个是一个字符串变量,字符串的值可以被修改

        char *s2 = "world!";      //这个是一个字符串常量,不能更改字符串的值

        

        //char s1[] = "hello ";

        //char s2[] = "world!";

        char const *pt = s1;       //始终不改变pt的指向,pt一直指向s1的首地址

        

        Pointer_stringcat(s1,s2);  //调用自定义的字符串连接函数

        puts(pt);

        

        return 0;

    }

     

  • 相关阅读:
    Python 文件的输入与输出
    php获取客户端真实ip
    php设计模式(3)-观察者模式
    php设计模式(2)-单例模式
    php设计模式(1)-工厂模式
    设计模式
    设计模式-观察者模式
    include和require的区别
    php分页类
    反向Ajax,第5部分:事件驱动的Web开发
  • 原文地址:https://www.cnblogs.com/XYQ-208910/p/4696561.html
Copyright © 2011-2022 走看看