C/C++实现串的操作,使用kmp算法来求子串,实现串的赋值,串的相加,串的比较,删除字符串,置换字符串,求串的长度

使用kmp算法来在母串中找到子串,这里面最重要的就是求next值
然后通过next数组来求出nextvall值
求next值: 一个字符串开始的第一个的next值都是为0,然后第二个值都为1,然后通过字符串的第二个字符和它的下标所值得字符进行比较,如果相同得话就将第二个字符得next值加1,如果不同得话就继续通过next值往前回溯。就这样一直往下执行,一直到最后一个字符。

例如
abaabcac

先给这串字符标上标
上标 12345678
abaabcac
next值 01122312
这就得到了这个串的next值

然后求nextvall的值
nextvall和next值不相同的就是next是不相同时候回溯,而nextvall值是在相同的时候回溯。然后如果不相同的话就将next写下来,要是相同的话就一直回溯,一直回溯到不相同

上标 12345678
abaabcac
next 01122312
nextvall 01021302

这就求得next值和nextvall的值

下面就是求字符串的操作,代码有注释



#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

using namespace std;

const int Lsize = 100;

int next[100];


//输入字符串

//求串长
int getListSize(char *pStr){

    int len=0;
    char *start = pStr;
    while(*start){
        len++;
        start++;
    }
    return len;
}


//串赋值
void AssignmentList(char *pStr,char *rStr){

     char *start = pStr;
     while(*rStr++ = *start++){
        ;
     }

}


//串连接

void connecList(char *pStr,char *conStr){

   printf(".........输入你想要连接的字符串\n");
   char cStr[Lsize];
   scanf("%s",&cStr);
   getchar();
   int len = getListSize(cStr);
   char *start = pStr;
   while(*start){
        *conStr = *start;
        start++;
        conStr++;
    }

    for(int i=0;i<len;i++){
        *conStr = cStr[i];
        conStr++;
    }
    *conStr = '\0';

}



//串比较
int cmps(char *arr1,char *arr2){

    int i=0,len=0;
    int size_arr1 = getListSize(arr1);
    int size_arr2 = getListSize(arr2);


    if(size_arr1 >= size_arr2){
        len = size_arr1;
    }else{

         len  = size_arr2;
    }

    for(i;i<len;i++){

        //如果两个字符相同且不为空的时候跳过这一次循环
        if((arr1[i]== arr2[i])&&(i != len-1)){
            continue;
        }
        //如果原字符串小于对比的字符串返回-1
        if(arr1[i]<arr2[i]){
            return -1;
        }
        //如果字符串同时到达最后则返回0
         if((arr2[i] == arr1[i])&&(i == len-1)){
            return 0;
        }
        //如果原字符串大于对比字符串返回1
        else{
            break;
        }

    }
       return 1;

}

//求Next值
void Next(char *p) {
	int j,k,n=17;
	next[0]=-1;
	j=0;
	k=-1;
	while(j<n-1) {
		if(k==-1||p[j]==p[k]) {
			j++;
			k++;
			next[j]=k;

		} else
			k=next[k];
	}
}


//KMP算法
int KMP(char *S,char *T) {
	int i = 0, j = 0;
	Next(T);

	while (S[i] != '\0' && T[j] != '\0') {
		if (S[i] == T[j]) {
			++i;
			++j;
		} else {
			j = next[j];
		}
		if (j == -1) {
			++i;
			++j;
		}
	}
	if (T[j] == '\0')
		return i - j;
	else
		return -1;
}

//求子串
int getSubString (char *pStr,char *cStr){

    int position;
    position = KMP(pStr,cStr);
    return position;
}


//删除
void deleteString(char *pStr,char *dStr){

   int position,dSize,i=0,pSize;
   printf(".........输入你想要删除的字符串\n");
   char cStr[Lsize];
   char tStr[Lsize];
   scanf("%s",&cStr);
   getchar();
   dSize = getListSize(cStr);
   pSize = getListSize(pStr);
   position = getSubString(pStr,cStr);

    while(*pStr){
        if(i==position){
            i = i+dSize;
           for(int j=0;j<dSize;j++){
            pStr++;
           }
        }
        *dStr = *pStr;
         dStr++;
         pStr++;
         i++;
    }
    *dStr = '\0';

}


//置换
void replaceString(char *pStr,char *rStr){

   int position,i=0,oneSize,twoSize,n=0;
   char RoneList[Lsize];
   char RtowList[Lsize];
   char last_list[Lsize];
   printf(".........输入被替换的字符\n");
   scanf("%s",&RoneList);
   getchar();
   printf(".........输入要替换的字符\n");
   scanf("%s",&RtowList);
   getchar();
   position = getSubString(pStr,RoneList);
   oneSize = getListSize(RoneList);
   twoSize = getListSize(RtowList);

    for(i;i<position;i++){
        *rStr = *pStr;
         pStr++;
         rStr++;

    }

    printf("%s",rStr);

    if(i==position){
            i = i+oneSize;
           for(int j=0;j<oneSize;j++){
            pStr++;
           }
        }
   while(*pStr){
          last_list[n] = *pStr;
          pStr++;
          n++;

    }
    last_list[n] = '\0';

    int len = getListSize(RtowList );
    for(int c=0;c<len;c++){
        *rStr = RtowList[c];
         rStr++;
    }

    for(int d=0;d<n;d++){
        *rStr = last_list[d];
         rStr++;
    }
    *rStr = '\0';
}



//连接
void RconnecList(char *pStr,char *conStr ){


   char cStr[Lsize];
   AssignmentList(conStr,cStr);
   memset(conStr, 0, sizeof conStr);
   int len = getListSize(cStr);
   char *start = pStr;
   while(*start){
        *conStr = *start;
        start++;
        conStr++;
    }
    for(int i=0;i<len;i++){
        *conStr = cStr[i];
         conStr++;
    }
    *conStr = '\0';

}



int main()
{
    char input_list[Lsize];//输入串
    char assignment_list[Lsize];//赋值串
    char connection_list[200];//串连接
    char compare_list[Lsize];//比较字符串
    char sub_list[Lsize];//子串
    char delete_list[Lsize];//删除字符串
    char replace_list[Lsize];//置换字符串
    char size_list[Lsize];//求串长度
    int note;//比较结果
    int list_Size;//串长度
    int position;//子串头位置

    do{
     printf("........................功能列表...................\n");
     printf("..............         1.字符串赋值  ................\n");
     printf("..............         2.字符串比较  ................\n");
     printf("..............         3.求字符串长度  ................\n");
     printf("..............         4.字符串连接  ................\n");
     printf("..............         5.求子串  ................\n");
     printf("..............         6.删除字符串  ................\n");
     printf("..............         7.置换字符串  ................\n");
     printf("..............         8.退出  ................\n");
     scanf("%d",&note);
        switch(note){
            case 1:
                memset(input_list, 0, sizeof input_list);
                memset(assignment_list, 0, sizeof assignment_list);
                printf("........输入想要赋值的字符串\n");
                scanf("%s",&input_list);
                getchar();
                AssignmentList(input_list,assignment_list);
                printf(".........赋值后的串为:%s\n",assignment_list);
            break;
            case 2:
                 memset(input_list, 0, sizeof input_list);
                 memset(compare_list, 0, sizeof compare_list);
                 printf("........输入比较的字符串1\n");
                 scanf("%s",&input_list);
                 getchar();
                 printf("........输入比较的字符串2\n");
                 scanf("%s",&compare_list);
                 getchar();
                 note = cmps(input_list,compare_list);
                 switch(note){

    case 1:
    printf("............%s这个字符串比较大\n",input_list);
    break;
    case 0:
    printf("............这两个字符串相同\n" );
     break;
    case -1:
    printf("............%s这个字符串比较大\n",compare_list);
    break;
    }
            break;
            case 3:
                memset(compare_list, 0, sizeof compare_list);
                printf("...........输入要求长度的字符串\n");
                scanf("%s",&size_list);
                getchar();
                list_Size = getListSize(size_list);
                printf("...........字符串长度:%d\n",list_Size);
            break;
            case 4:
                 memset(input_list, 0, sizeof input_list);
                // memset(assignment_list, 0, sizeof assignment_list);
                 memset(connection_list, 0, sizeof connection_list);
                 printf("........输入要连接的字符串1\n");
                 scanf("%s",&input_list);
                 getchar();
                 //printf("........输入要连接的字符串2\n");
                 //scanf("%s",&connection_list);
                // getchar();
                 connecList(input_list,connection_list);
                 printf("........连接后的字符串为:%s\n",connection_list);
            break;
            case 5:
                memset(input_list, 0, sizeof input_list);
                printf(".........输入母串\n");
                scanf("%s",&input_list);
                getchar();
                memset(sub_list, 0, sizeof sub_list);
                printf(".........输入想要求的子串\n");
                scanf("%s",&sub_list);
                getchar();
                position = getSubString(input_list,sub_list)+1;
                if(position>0)
                printf(".........子串头节点:%d\n",position);
                else
                 printf(".........没有这个子串\n");
            break;
            case 6:
                memset(input_list, 0, sizeof input_list);
                printf(".........输入母串\n");
                scanf("%s",&input_list);
                getchar();
                memset(delete_list, 0, sizeof delete_list);
                deleteString(input_list,delete_list);
                printf(".........删除后字符串为:%s\n",delete_list);
            break;
            case 7:
                memset(input_list, 0, sizeof input_list);
                printf(".........输入母串\n");
                scanf("%s",&input_list);
                getchar();
                replaceString(input_list,replace_list);
                printf("..........替换后的字符串%s\n",replace_list);
                break;
            case 8:
            exit(0);
        }

    }while(note<8);
    return 0;
}

有错误的欢迎指出,有不清楚的可以留言

发布了34 篇原创文章 · 获赞 15 · 访问量 3236

猜你喜欢

转载自blog.csdn.net/weixin_43404016/article/details/94393096
今日推荐