赞
踩
- int str_len(char s[]){
- int i=0;
- while(s[i]!='\0'){
- i++;
- }
- return i;
- }
- int str_cat(char s1[],char s2[]){
- int len1=str_len(s1);
- int len2 =str_len(s2);
- int i,j;
- if(len1+len2>MAXSIZE-1){
- puts("超过最大范围!!!");
- return 0;
- }else{
- i=len1;
- j=0;
- while(s2[j]!='\0'){
- s1[i++]=s2[j++];
- }
- s1[i]='\0';
- }
- return 1;
- }
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
- //将s中从第i个位置开始,长度为len的子串通过ss返回
- int sub_str(char s[],char ss[],int i,int len){
- int slen=str_len(s);
- if(i<1||i>slen||len<0||len>slen-i+1){
- puts("参数有误!!!");
- return 0;
- }else{
- int j;
- for(j=0;j<len;j++){
- ss[j]=s[i+j-1];
- }
- }
- return 1;
- }
- int str_cmp(char s1[],char s2[]){
- int i=0;
- while(s1[i]==s2[i]&&s1[i]!='\0'){
- i++;
- }
- return s1[i]-s2[i];
- }
- //将串s2插入到s1中i的位置
- int str_insert(char s1[],int i,char s2[]){
- int len1=str_len(s1);
- int len2=str_len(s2);
- int j,k;
- char temp[10];
- if(i<0||i>len1+1||len2+len1>MAXSIZE-1){
- puts("参数有误!!!");
- return 0;
- }else{
- k=i;
- for(j=0;s1[k]!='\0';j++,k++){
- temp[j]=s1[k];
- }
- temp[j]='\0';
- j=0;
- while(s2[j]!='\0'){
- s1[i++]=s2[j++];
- }
- j=0;
- while(temp[j]!='\0'){
- s1[i++]=temp[j++];
- }
- s1[i]='\0';
- }
- return 1;
- }
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
- void init_Str(LinkStr **str,char s[]){
- LinkStr *p,*q;
- *s=(LinkStr *)malloc(sizeof(LinkStr));
- q=*s;
- int i,j;
- for(i=0;s[i]!='\0';i++){
- p=(LinkStr *)malloc(sizeof(LinkStr));
- p->data=s[i];
- q->next=p;
- q=p;
- }
- q->next=NULL;
- }
- int str_Length(LinkStr *s){
- int i;
- LinkStr *p=s->next;
- while(p!=NULL){
- i++;
- p=p->next;
- }
- return i;
- }
- void strCat(LinkStr *s,LinkStr *t){
- LinkStr *p,*q,*str,*r;
- str=(LinkStr *)malloc(sizeof(LinkStr));
- r=str;
- p=t->next;
- while(p!=NULL){//先将t拷贝到临时变量str中:
- q=(LinkStr *)malloc(sizeof(LinkStr));
- q->data=p->data;
- r->next=q;
- r=q;
- p=p->next;
- }
- p=s;
- while(p->next!=NULL){//找到s的尾指针
- p=p->next;
- }
- p->next=str->next;//进行链接
- free(str);
- }
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
- void strSub(LinkStr *f,LinkStr **s,int i,int len){
- LinkStr *p,*q,*r;
- int k;
- p=f->next;//第一个元素节点
- *s=(LinkStr *)malloc(sizeof(LinkStr));
- *s->next=NULL;
- r=*s;
- if(i<0||i>str_Length(f)||len>str_Length(f)-i+1||len<0){
- puts("参数错误!!!");
- }else{
- for(k=0;k<i-1;k++){//找到i位置结点的前驱结点
- p=p->next;
- }
- for(k=0;k<len;k++){
- q=(LinkStr *)malloc(sizeof(LinkStr));
- q->data=p->data;
- r->next=q;
- r=q;
- p=p->next;
- }
- r->next=NULL;
- }
- }
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
- void strInsert(LinkStr *s,int i,LinkStr *t){
- LinkStr *p,*q;
- p=s->next;//第一个元素节点
- q=t;
- int k;
- for(k=0;k<i-1;k++){//找到i位置结点的前驱结点
- p=p->next;
- }
- r=p->next;//将i元素结点位置的串暂时存在r中
- p->next=t->next;//将t中的第一个元素链接到i-1的后面,即i的位置
- p=t;
- while(p->next!=NULL){//寻找t的尾结点
- p=p->next;
- }
- p->next=r;
- }
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。