• #include<stdio.h>
    #include<stdlib.h>
    #define MAXLEN 100
    typedef struct{
        char * vec;
        int length;
    }str;

    int StrLength(char* obj){//求串长
       int i=0,n=0;
       while(obj[i]!=''){
            i++;
          n++;
       }
    return n;
    }

    void InitStr(str *obj1){//初始化串
         obj1->vec=NULL;
         obj1->length=0;
        }
    void CreateStr(str *obj1,char *obj2){//创建串
          int i=0;
          obj1->vec=(char *)malloc(StrLength(obj2)*sizeof(char));
          for(i=0;i<StrLength(obj2);i++){
            obj1->vec[i]=obj2[i];
            obj1->length++;
          }
    }


    int FindStr(str *obj1,char *obj2,int pos){//查找子串位置
        int i=pos,j=0;
        if(pos<0||pos>obj1->length+1){
            printf("输入有误");
            return 0;
    }
    while(i<=obj1->length-1&&j<=StrLength(obj2)-1){
        if(obj1->vec[i]==obj2[j]){
            ++i;
            ++j;
        }
        else{
            i=i-j+1;
            j=0;
        }
    }
    if(j>StrLength(obj2)-1){
        return i-StrLength(obj2)+1;
    }else
        return 0;
    }


    void ShowStr(str *obj){//遍历结构体
        int i=0;
      for(i=0;i<obj->length;i++){
        printf("%c",obj->vec[i]);
      }
      printf(" ");
    }

    void ShowStr2(char *obj){//遍历普通串
        int i=0;
     for(i=0;i<StrLength(obj);i++){//
        printf("%c",obj[i]);
     }
     printf(" ");
    }

    void ReverseStr(str *obj1,char *obj2){//逆序
        int i=0;
        memcpy(obj2,obj1->vec,obj1->length*sizeof(char));
        for(i=0;i<obj1->length;i++){
            obj1->vec[i]=obj2[obj1->length-1-i];
        }
    }

    void ReplaceStr(str *obj1,char *obj2,char *obj3,int pos){//替换
        int i;int n;int j;int k;int obj2_NextChar;int obj3_NextChar;
        n=FindStr(obj1,obj2,pos);//查找到的位置
        j=StrLength(obj2);
        k=StrLength(obj3);
        obj2_NextChar=n-1+j;//被替换的字符串的下一字符的数组下标
        obj3_NextChar=n-1+k;//替换后字符的下一字符的数组下标
        if(k<=j){//替换的字符串比被替换的字符串短
                for(i=0;i<k;i++){//先替换
                    obj1->vec[n-1+i]=obj3[i];
                }
               for(i=0;i<(obj1->length-(obj2_NextChar));i++){//然后前移
                   obj1->vec[obj3_NextChar+i]=obj1->vec[obj2_NextChar+i];
                    }
                obj1->length-=(j-k);
        }else{
        for(i=0;i<obj1->length-obj2_NextChar;i++){//先后移
                obj1->vec[obj1->length-1-i+(k-j)]=obj1->vec[obj1->length-1-i];
            }
        for(i=0;i<k;i++){//替换
            obj1->vec[n-1+i]=obj3[i];
        }
        obj1->length+=(k-j);
    }
    }

    void DeleteStr(str *obj1,char *obj2,int pos){//删除
        int begin;
        int i;
        int obj2_length=StrLength(obj2);
        begin=FindStr(obj1,obj2,pos-1);
        begin=begin-1;//数组中的位置
        for(i=0;i<obj1->length-begin-obj2_length+1;i++){
                obj1->vec[begin+i]=obj1->vec[begin+obj2_length+i];
        }
    obj1->length-=obj2_length;
    }

    void  CompareStr(str *obj1,char *obj2){//比较串大小
        int i;
        printf("大的字符串为 ");
         (obj1->length)>(StrLength(obj2))?ShowStr(obj1):ShowStr2(obj2);
    }

    void ConnectStr(str *obj1,char *obj2){//串连接
        int i=0;
        for(i=0;i<StrLength(obj2);i++){
            obj1->vec[obj1->length+i]=obj2[i];
        }
        obj1->length+=StrLength(obj2);
    }

    void InsertStr(str *obj1,char *obj2,int index){//串插入
       int i=0;
       for(i=0;i<obj1->length-index;i++){//向后移
        obj1->vec[obj1->length-1-i]=obj1->vec[obj1->length-1+StrLength(obj2)-i];
         }
       for(i=0;i<StrLength(obj2);i++){//替换
        obj1->vec[index+i]=obj2[i];
        }
        obj1->length+=StrLength(obj2);
        printf("长度为%d",obj1->length);
    }

    int main(void){
        int i=0;int n; int z=0;int m;int x;int t=1;static int h;static int count=1;int k;int index;
       str *obj=( str*)malloc(sizeof(str));;
       char t1[MAXLEN];
        char t2[MAXLEN];
        char t3[MAXLEN];
        char t4[MAXLEN];
        char t5[MAXLEN];
        char t6[MAXLEN];
        char t7[MAXLEN];
        char t8[MAXLEN];
         char t9[MAXLEN];
       while(t){
        printf(" 欢迎来到串处理程序 ");
        printf(" ******************************* ");
        printf(" 1.初始化串 ");
        printf(" 2.创建串 ");
        printf(" 3.遍历串 ");
        printf(" 4.查找子串 ");
        printf(" 5.逆序串 ");
        printf(" 6.替换串 ");
        printf(" 7.删除串 ");
        printf(" 8.比较串大小串 ");
        printf(" 9.连接串 ");
        printf(" 10.插入串 ");
        printf(" 11.退出 ");
        printf("请输入你要进行的操作: ");
         fflush(stdin);
        scanf("%d",&x);
        switch(x){
                case 1:
                            InitStr(obj);
                           printf("初始化串完毕 ");
                           break;
                case 2:
                          printf("请输入字符串: ");
                          fflush(stdin);
                         gets(t1);
                          n=StrLength(t1);
                          CreateStr(obj,t1);
                          printf("创建串完毕 ");
                          break;
                case 3:
                         ShowStr(obj);
                         break;
                 case 4:
                         printf("请输入需要查找的字符串 ");
                         fflush(stdin);
                         gets(t2);
                         printf("请输入从第几个位置开始查找 ");
                        fflush(stdin);
                         scanf("%d",&z);
                         z=z-1;
                      while(1){
                                 m=FindStr(obj,t2,z);
                                if(m==0){
                               break;
                      }
                      printf("找到了在%d ",m);
                      z=FindStr(obj,t2,z);
                     m=z;
                     }
                     break;
                 case 5:
                     printf("倒序后 ");
                     ReverseStr(obj,t3);
                     ShowStr(obj);
                     break;
                 case 6:
                         printf("请输入需要替换的字符串 ");
                         fflush(stdin);
                         gets(t4);
                          printf("请输入替换后的字符串 ");
                         fflush(stdin);
                         gets(t5);
                         printf("请输入从第几个位置开始替换 ");//这个功能出错了,不能局部替换了,只能全局替换,懒得改了,心累
                        fflush(stdin);
                         scanf("%d",&h);
                         while( 1){
                             if(FindStr(obj,t4,h)!=0){
                              ReplaceStr(obj,t4,t5,h);
                             h=FindStr(obj,t4,h);
                             }else{
                             break;
                             }


                         }
                        ShowStr(obj);
                        break;
                 case 7:
                     printf("请输入你要删除的串 ");
                     fflush(stdin);
                     gets(t6);
                     printf("请输入你要删除的起始位置: ");
                     fflush(stdin);
                     scanf("%d",&k);
                      while( FindStr(obj,t6,k)){
                        DeleteStr(obj,t6,k);
                      }
                      ShowStr(obj);
                     break;
                 case 8:
                     printf("请输入你要比较大小的字符串 ");
                     fflush(stdin);
                     gets(t7);
                    CompareStr(obj,t7);
                    break;
                 case 9:
                     printf("请输入你要连接的字符串 ");
                     fflush(stdin);
                     gets(t8);
                     ConnectStr(obj,t8);
                     ShowStr(obj);
                     break;
                case 10:
                    printf("请输入插入的位置 ");
                     scanf("%d",&index);
                     printf("请输入你要插入的字符串 ");
                     fflush(stdin);
                     gets(t9);
                     InsertStr(obj,t9,index);
                     ShowStr(obj);
                     break;
                case 11:
                     t=0;
                     break;
        }

        }

     return 0;
    }

  • 相关阅读:
    jquery获取对象的方法足以应付常见的各种类型的对象
    jquery如何判断表格同一列不同行input数据是否重复
    老司机带你解读jQuery插件开发流程
    jQuery插件开发详细教程
    jquery动态调整div大小使其宽度始终为浏览器宽度
    jQery使网页在显示器上居中显示适用于任何分辨率
    R语言学习——数据框
    java实验五——字符数组、String、StringBuffer的相互转化,StringBuffer的一些方法
    java实验五——字符数组、String、StringBuffer的相互转化,StringBuffer的一些方法
    java实验四——找鞍点
  • 原文地址:https://www.cnblogs.com/wantao/p/7828606.html
Copyright © 2020-2023  润新知