zoukankan      html  css  js  c++  java
  • 数据结构基础_插入字符串

    源代码

    /*
     * strcat2.c
     *
     *  Created on: 2013-4-13
     *      Author: yeahwell
     */
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define MAX_SIZE 100
    
    
    /**
     * 把src字符串插入到dest字符串的第position个位置
     * @param dest  目标串
     * @param src   源串
     * @param position 所要插入的位置
     */
    void strcat2(char *dest, char *src, int position);
    
    int main(){
    	char string1[MAX_SIZE] = "amobile", *dest = string1;
    	char string2[MAX_SIZE] = "uto", *src = string2;
    	int position = 1;
    	strcat2(dest, src, position);
    	printf("连接后的字符串为%s", dest);
    	return 0;
    }
    
    void strcat2(char *dest, char *src, int position){
    	char string3[MAX_SIZE], *temp = string3;
    	if(position < 0 || position > strlen(dest)){
    		fprintf(stderr, "所要插入的位置溢出边界");
    		exit(EXIT_FAILURE);
    	}
    	if(!strlen(dest)){   //如果目标串的长度为0,则直接copy源字符窜
    		strcpy(dest, src);
    	}else if(strlen(src)){   //
    		strncpy(temp, dest, position); //复制dest的前position个字符到temp中
    		strcat(temp, src);             //连接temp和src
    		strcat(temp, (dest + position));  //连接dest的position位置之后的字串
    		strcpy(dest, temp);           //复制temp字符窜到dest中
    	}
    
    }



    运行结果:

    连接后的字符串为automobile




    作者:沙漏哟
    出处:计算机的未来在于连接
    本文版权归作者和博客园共有,欢迎转载,请留下原文链接
    微信随缘扩列,聊创业聊产品,偶尔搞搞技术
  • 相关阅读:
    RESTful规范1
    Django -- 发送HTML格式的邮件
    11.10 vue
    Selenium 使用
    Beautiful Soup的用法
    Pthon常用模块之requests,urllib和re
    爬虫--工具安装Jupyter anaconda
    11-3
    Python -- tabulate 模块,
    Python -- queue队列模块
  • 原文地址:https://www.cnblogs.com/yeahwell/p/5226020.html
Copyright © 2011-2022 走看看