串的基本用法(顺序存储结构)
来源:互联网 发布:贴贴相传网络曝光 编辑:程序博客网 时间:2024/09/21 09:21
#include <stdio.h>#include <stdlib.h>#include <malloc.h>#define MAXSIZE 10#define DataType chartypedef struct{ DataType ch[MAXSIZE]; int length;}SString;SString *CreatString(void);int GetLength(SString *S);int SStrInsert(SString *S,int pos,const SString *T);int SStrDelete(SString *S,int pos,int len);int SStrCat(SString *S,const SString *T);int SubSString(SString *T,SString *S,int pos,int len);void PrintString(SString *S);void Destroy(SString *S);SString *CreatString(void){ SString *S=NULL; S=(SString *)malloc(sizeof(SString)); S->length=0; scanf("%s",S->ch); S->length=GetLength(S); return S;}int GetLength(SString *S){ int i=0; while(S->ch[i]!='\0') { i++; } return i;}int SStrInsert(SString *S,int pos,const SString *T){ int length_T=GetLength(T); int length_S=GetLength(S); int i=0; int j=0,k=0,p=0,q=0; if(pos<=0||pos>=MAXSIZE) { printf("Illegal insert location.\n"); return 1; } else { i=pos-1+length_T; k=pos-1; if(length_S+length_T<=MAXSIZE) { p=length_S-pos+1; q=length_T; } else if(length_T+pos<=MAXSIZE) { q=length_T; p=MAXSIZE-length_T-pos+1; } else { q=MAXSIZE-pos+1; p=0; } for(j=0;j<p;j++) { S->ch[i+p-1]=S->ch[k+p-1]; i--; k--; } i=pos-1; k=0; for(j=0;j<q;j++) { S->ch[i]=T->ch[k]; i++; k++; } S->length=GetLength(S); } return 0;}int SStrDelete(SString *S,int pos,int len){ int i=0; int length=0; int k=0; if(pos<=0 || pos>GetLength(S) || NULL==S || len<=0 || len>GetLength(S)) { printf("Illeagel input.The program ends.\n"); exit(0); } else { if(pos+len<=MAXSIZE) length=len; else { length=GetLength(S)-pos-1; } k=pos-1; for(i=0;i<length;i++) { S->ch[k]=S->ch[k+len]; k++; } S->length=GetLength(S); return 0; }}int SStrCat(SString *S,const SString *T){ int pos=GetLength(S); int i=0; for(i=0;i<GetLength(T);i++) { if(pos==MAXSIZE) break; else { S->ch[pos]=T->ch[i]; pos++; } } S->length=GetLength(S); return 0;}int SubSString(SString *T,SString *S,int pos,int len){ int i=0; int k=pos-1; if(pos<=0 || pos>=GetLength(S) || NULL==S || len<=0 || len>GetLength(S)) { printf("Illeagel input.The program ends.\n"); exit(0); } else { for(i=0;i<len;i++) { if(k==GetLength(S)) { break; } else { printf("%c ",S->ch[k]); k++; } } return 0; }}void PrintString(SString *S){ int i=0; while(S->ch[i]!='\0') { printf("%c",S->ch[i]); i++; } printf("\n");}void Destroy(SString *S){ free(S); S=NULL;}int main(){ SString *s,*t; SString *t_2,*t_3=NULL ; printf("Please Input a string ,the max length is %d.\n",MAXSIZE); s=CreatString(); printf("Please Input a waiting for being inserted string.\n"); t=CreatString(); SStrInsert(s,5,t); printf("After insert a string,the new string is:\n"); PrintString(s); SStrDelete(s,4,4); printf("After delect a string,the new string is:\n"); PrintString(s); printf("Please input a string that needs to be chained.\n"); t_2=CreatString(); SStrCat(s,t_2); printf("After chaining the two strings,the new strings is:\n"); PrintString(s); SubSString(t_3,s,3,4); Destroy(s); return 0;}
0 0
- 串的基本用法(顺序存储结构)
- 串的基本用法(顺序存储结构)
- 栈的基本用法(顺序存储结构)
- 队列的基本用法(顺序存储结构)
- 共享栈基本用法(顺序存储结构)
- 串采用定长顺序存储结构的基本操作
- 串采用定长顺序存储结构的基本操作
- 线性表(顺序存储结构)的基本操作
- 字符串顺序存储结构的基本运算
- 串的顺序存储结构
- 串的顺序存储结构
- 串的顺序存储结构
- 串的顺序存储结构
- 栈的基本用法(链式存储结构)
- 循环队列的基本用法(链式存储结构)
- 队列的顺序存储结构及其基本运算的实现
- 队列的顺序存储结构及其基本运算的实现
- 线性表的顺序存储结构的基本操作
- R软件学习笔记-3(简单运算)
- vim中的常用命令
- 黑马程序员-----网络编程
- 在linux下连接MS SQL Server2008中SQL语句like中文
- [Leetcode 45, Hard] Jump Game II
- 串的基本用法(顺序存储结构)
- Android开发者必须深入学习的10个应用开源项目
- 前端测试系列---如何分析页面性能
- Linux中跳过的章节
- 在MFC中从一个线程工作函数中向窗口发送消息以及窗口到窗口消息传送方法
- 深入解析MFC -- CWinApp
- eclipse下Drools 6.x 开发环境搭建
- phonegap研究小结
- 修改ios中statusbar字体颜色