#include <stdio.h>
#include <tchar.h>
#include <stdlib.h> // TODO: 在此处引用程序需要的其他头文件 struct String{
char* ch;
int length;
}; bool Assign_String(String* str, char* chars);
bool Destroy_String(String* str);
bool Clear_String(String* str);
void Print_String(String str);
bool Insert_String(String* str, String T, int locate);
bool Copy_String(String* str, String T);
int Index_String(String str, String T); //给字符串重新赋值
bool Assign_String(String* str,char* chars){
if (str->ch) free(str->ch);
int i;
char* c;
for (i = , c = chars; *c; c++, i++);//获取字符串长度
if (!i) { str->ch = NULL; str->length = ; return true; }
else{
str->ch = (char*)malloc(i*sizeof(char));
for (int j = ; j < i; j++){
str->ch[j] = chars[j];
}
str->length = i;
return true;
}
}
//销毁字符串
bool Destroy_String(String* str){
if (str)
{
free(str);
str = NULL;
return true;
}
else
return false;
}
//清空字符串
bool Clear_String(String* str){
while (str->length){
str->ch[str->length] = NULL;
str->length--;
}
return true;
}
//打印
void Print_String(String str){
if (str.length)
{
for (int i = ; i < str.length;i++)
{
printf("%c", str.ch[i]); }
printf("\n");
}
}
//在原字符串str的第locate个元素前插入子字符串T
bool Insert_String(String* str, String T, int locate){
if (locate< || locate>str->length) return false;
if (T.length){
if (!(str->ch = (char*)realloc(str->ch,(str->length + T.length)*sizeof(char)))) exit(-);
for (int i = str->length - ; i >= locate; i--)
str->ch[i + T.length] = str->ch[i];
for (int i = ; i < T.length; i++)
str->ch[locate + i] = T.ch[i];
str->length += T.length;
}
return true;
}
//把T的值赋给str
bool Copy_String(String* str, String T){
if (str->ch) free(str->ch);
if (!&T) { str->ch = NULL; str->length = ; return true; }
else{
str->ch = (char*)malloc(T.length*sizeof(char));
for (int i = ; i < T.length; i++){
str->ch[i] = T.ch[i];
}
str->length = T.length;
}
return true;
}
//在原字符串str中搜索子字符串T的位置
//查找失败返回-1
//正常返回index为第一次出现T首字符的位置
int Index_String(String str, String T){
int index = -;
int key = ;
int Hl = ;
if (T.length&&str.length>=T.length){
for (int i = ; i <= str.length - T.length; i++){
if (str.ch[i] == T.ch[key]){
key++;
Hl++;
if (Hl >= T.length) { printf("出来了\n"); index = i - T.length+; return index; }
}
else{
Hl = key = ;
}
printf("i=%d,str是%c,T是%c,核对T的第%d个字符,已满足字符数%d\n", i, str.ch[i], T.ch[key], key, Hl);
}
}
return index;
}

c语言实现基本的数据结构(六) 串的更多相关文章

  1. hdu 3336:Count the string(数据结构,串,KMP算法)

    Count the string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  2. 数据结构(C语言版)-第4章 串、数组和广义表

    补充:C语言中常用的串运算 调用标准库函数 #include<string.h> 串比较,strcmp(char s1,char s2) 串复制,strcpy(char to,char f ...

  3. 《数据结构与算法分析:C语言描述》复习——第六章“排序”——冒泡排序

    2014.06.17 01:04 简介: 冒泡排序是O(n^2)级别的交换排序算法,原理简单,属于必知必会的基础算法之一. 思路: 排序要进行N轮,每一轮从尾部逐个向前扫描,遇到逆序对就进行交换.确保 ...

  4. Go语言备忘录:基本数据结构

    本文内容是本人对Go语言的变量.常量.数组.切片.映射.结构体的备忘录,记录了关键的相关知识点,以供翻查. 文中如有错误的地方请大家指出,以免误导!转摘本文也请注明出处,多谢! 参考书籍<Go语 ...

  5. R语言入门 :基本数据结构

    1.向量 向量是R语言中最基本的数据类型,在R语言中没有单独的变量. (1)  创建向量 R语言中可以用 = 或者 <- 来赋值. 向量名 <- 向量 或  向量名 = 向量 向量的创建方 ...

  6. C#数据结构之串

    串(string)是n(n>=0)个字符组成的有限序列. 由于串中的字符都是连续存储的,在C#中有恒定不变的特性.一经创建就保持不变. 为了区别C#中的string,因此以stringDS类模拟 ...

  7. 算法与数据结构(六) 迪杰斯特拉算法的最短路径(Swift版)

    上篇博客我们详细的介绍了两种经典的最小生成树的算法,本篇博客我们就来详细的讲一下最短路径的经典算法----迪杰斯特拉算法.首先我们先聊一下什么是最短路径,这个还是比较好理解的.比如我要从北京到济南,而 ...

  8. 【Java】 大话数据结构(8) 串的模式匹配算法(朴素、KMP、改进算法)

    本文根据<大话数据结构>一书,实现了Java版的串的朴素模式匹配算法.KMP模式匹配算法.KMP模式匹配算法的改进算法. 1.朴素的模式匹配算法 为主串和子串分别定义指针i,j. (1)当 ...

  9. 数据结构(C语言)分享笔记:数据结构的逻辑层次、存储层次

    [1] 严格意义上数据结构的概念 数据结构,一个简单的定义:相互之间存在一种或多种特定关系的数据元素的集合.即:数据结构 = 元素集合 + 元素间关系的集合 . 在讨论数据结构时,可以基于两个不同的层 ...

随机推荐

  1. 【题解】P2078 朋友-C++

    题目传送门 这道题目就是一个模板并查集 但是!唯一不同的地方在于,这道题的编号有负数. C++的map你忘了吗!!!下表可以是任意类型. 所以把fa数组开成一个int->int的map就可以了 ...

  2. css3系列之弹性盒子 flex

    弹性盒子(伸缩盒) 注意,本篇会很长,非常长, 因为弹性盒子的知识点比较多 搜索 弹性盒子的属性  ctrl + F   如果觉得图太小, ctrl + +键 设置弹性盒子的属性: display:f ...

  3. vs2010编译zapline-zapline.systemoptimization 注释工程中的//#define abs(value) (value >= 0 ? value : -(value))即可

    vs2010编译zapline-zapline.systemoptimization-8428e72c88e0.zip出错 1>d:\program files (x86)\microsoft ...

  4. E11000 duplicate key error index

    E11000 duplicate key error index mongodb插入报错,重复主键问题,有唯一键值重复 一般使用collection.insertOne(doc);插入一条已存在主键的 ...

  5. [PTA] 数据结构与算法题目集 6-8 求二叉树高度 & 6-9 二叉树的遍历

    6.8 二叉树高度 int GetHeight(BinTree BT) { if (BT == NULL) return 0; int leftH = GetHeight(BT->Left); ...

  6. Visual Studio 调试系列1 Debug 与 Release 模式

    系列目录     [已更新最新开发文章,点击查看详细] Debug 模式 Debug 通常称为调试版本,它包含调试信息,并且不作任何优化,便于程序员调试程序. 在Debug模式下调试,可以在断点处看到 ...

  7. python的乘法口诀表

    python的乘法口诀表 python的乘法口诀表 用python来写一个脚本,使得这个脚本在运行后自动输出乘法口诀表. pyton的脚本如下: #!/usr/bin/env python #codi ...

  8. Charles(Windows/Android)入门使用

    一. 介绍以及下载(windows) Charles是一个HTTP代理/HTTP监视器/反向代理,使开发人员能够查看其机器和Internet之间所有HTTP和SSL/HTTPS流量,这包括请求,响应和 ...

  9. 脱壳系列_2_IAT加密壳_详细版解法1(含脚本)

    1 查看壳程序信息 使用ExeInfoPe 分析: 发现这个壳的类型没有被识别出来,Vc 6.0倒是识别出来了,Vc 6.0的特征是 入口函数先调用GetVersion() 2 用OD找OEP 拖进O ...

  10. 洛谷 P3811 题解

    题面 利用暴力快速幂O(nlogn)会TLE掉: 所以对于求1~n的所有逆元要用递推公式: #include <bits/stdc++.h> using namespace std; ]; ...