线性结构之串

来源:互联网 发布:计算机中文编程 编辑:程序博客网 时间:2024/06/08 08:08

线性结构之串

字符串是一种特殊的线性表,它的特殊性在于线性表的数据元素限定为字符串。

1. 几个常用的操作

  • 得到串的长度
  • 截取字符串
  • 比较两个串是否相等
  • 串连接
  • 删除子串
#ifndef _VSTRING_H_#define _VSTRING_H_#include<stdio.h>#include<string.h>#include<malloc.h>#define MAXSIZE 100int strLength(char *s);             //得到串的长度 void subString(char *opt, char* res, int left, int right);  //截取字符串 int strCompare(char *opt, char *res);           //比较两个串是否相等void strCat(char *res, char* opt);              //将串opt连接在res之后 void strAssign(char *res, char* opt);           //使串res为删除子串opt后的串值 void strAssign(char *res, char* opt) {    char *p, *q;    p = strtok(res, opt);    q = strtok(NULL, opt);    strCat(p, q);    res = p;}void strCat(char *res, char* opt) {    strcat(res, opt);}int strCompare(char *opt, char *res) {    return strcmp(opt, res);   //返回值等于0的时候说明两个串是相等的 }void subString(char *opt, char* res, int left, int right) {    int num = strLength(res);    int i, j = 0;    for(i = left; i < right; i++) {        opt[j++] = res[i];    }    opt[j] = '\0';}int strLength(char *s) {    return strlen(s);}#endif

2. 找到串S中串T的位置,并返回次数

#include "vstring.h"int deleteSubstring(char *s, char *t);int deleteSubstring(char *s, char *t) {    int i = 0, n = 0, k = 0;     char tmp[MAXSIZE];    int res[MAXSIZE];     //如果t的长度比s还大,就出错了    int len1 = strLength(s);    int len2 = strLength(t);    if(len1 < len2) {        return -1;  //出错     }     //在s中可以划分出len1-len2+1个长度等于t的子串     while(i < len1-len2+1) {        subString(tmp, s, i, i+len2);        if(strCompare(tmp, t) == 0) {            res[k++] = i;            n++;        }        i++;    }       for(i = 0; i < k; i++) {        printf("%d ", res[i]);    }    printf("\n");    return n;   }void main(void) {    char s[MAXSIZE] = "abcdefgbcde78bcde223434bcde99";    char t[MAXSIZE] = "bcde";    int num = deleteSubstring(s, t);    printf("%d\n", num);}

3. 修改常用的操作strAssign

之前的strAssign函数只能用去在S字符串只出现一次T的情况,当S中的T的次数多于1时,上面的函数就不适用了,现将该函数修改如下:

void strAssign(char *res, char* opt);void strAssign(char *res, char* opt) {    int num;    char head[MAXSIZE], tail[MAXSIZE];    char *index = strstr(res, opt);     num = strLength(res) - strLength(index);    subString(head, res, 0, num);    subString(tail, res, num+strLength(opt), strLength(res));    strCat(head, tail);    strcpy(res, head);}

4. 完成在S串中删除T串的操作

#include "vstring.h"int deleteSubstring(char *s, char *t);int deleteSubstring(char *s, char *t) {    int i = 0, n = 0, k = 0;     char tmp[MAXSIZE];    int res[MAXSIZE];    char ss[MAXSIZE];     strcpy(ss, s);    //如果t的长度比s还大,就出错了    int len1 = strLength(s);    int len2 = strLength(t);    if(len1 < len2) {        return -1;  //出错     }     //在s中可以划分出len1-len2+1个长度等于t的子串     while(i < len1-len2+1) {        subString(tmp, s, i, i+len2);        if(strCompare(tmp, t) == 0) {            res[k++] = i;            strAssign(ss, t);            n++;        }        i++;    }       for(i = 0; i < k; i++) {   //输出下标         printf("%d ", res[i]);    }    printf("\n%s\n", ss);    return n;   }void main(void) {    char s[MAXSIZE] = "abcdefgbcde78bcde223434bcde99";    char t[MAXSIZE] = "bcde";    int num = deleteSubstring(s, t);    printf("%d\n", num);}

在上面的代码上添加了一些输出的提示内容,结果如下:
这里写图片描述

虽然折腾的时间比预期的有点长,但还是实现了,代码下载地址: http://download.csdn.net/detail/dear_mr/9815973

1 0
原创粉丝点击