数据结构基础_插入字符串
2013-04-13 10:09
169 查看
源代码
运行结果:
连接后的字符串为automobile
/* * 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
相关文章推荐
- 数据结构基础_插入字符串
- 【哈工大oj】1551 - 基础数据结构——字符串2 病毒II(水)
- 数据结构与算法之基础排序(冒泡/插入/选择)<十>
- Python基础学习----数据类型,字符串,内置数据结构
- python基础数据结构——列表(list), 元祖(tuple), 字典(dict), 字符串(string), 集合(set) 介绍及相互转换
- 数据结构基础5.2:二叉搜索树(BST)的基本操作(插入、查找、删除)
- 数据结构基础(11)------------输出字符串的所有组合( Permutation)--腾讯笔试附加题
- 【程序员面试宝典】数据结构基础一单链表:创建|求长|插入|删除|排序|打印|逆置
- 字符串-英语数字转换器(数据结构基础 第4周)
- 字符串-前缀中的周期(数据结构基础 第4周)
- Java之数据结构基础、线性表、栈和队列、数组和字符串,树—学习笔记
- PJLIB库基础框架-数据结构之字符串的使用
- Java之美[从菜鸟到高手演变]之数据结构基础、线性表、栈和队列、数组和字符串
- 数据结构和算法 (二)数据结构基础、线性表、栈和队列、数组和字符串
- [数据结构与算法]第一周(一):线性表---多项式加法&&字符串插入
- 字符串-KMP算法实现(数据结构基础 第4周)
- 2、数据结构和设计模式(数据结构基础、字符串、设计模式与软件测试)
- Java之美[从菜鸟到高手演变]之数据结构基础、线性表、栈和队列、数组和字符串
- 数据结构——单链表的创建、逆置、插入、有序表的建立、有序单链表合并等基础操作!!
- 字符串-字符串类的实现(数据结构基础 第4周)