#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;
}

最新文章

  1. hdu 4481 Time travel(高斯求期望)(转)
  2. table 细边框
  3. cordova开发问题汇总
  4. 通过Nginx,Tomcat访问日志(access log)记录请求耗时
  5. 这只是一篇用Markdown写的随记,就是熟悉熟悉MarkDown而已
  6. 百度ueditor学习使用
  7. I2S和PCM
  8. Android 自定义ToggleButton+用SharedPreferences保存用户配置
  9. 企业级搜索引擎Solr使用入门指南
  10. chrome浏览器如何在本地安装谷歌访问助手教程
  11. openlayers应用(二):加载百度离线瓦片
  12. ISLR系列:(1)线性回归 Linear Regression
  13. Asible第三章:roles--小白博客
  14. [PA2014]Druzyny
  15. FIT2096 Assignment 2 2019
  16. 《Java编程思想》读书笔记-对象导论
  17. 延续(continuation)
  18. Jmeter学习(三十二)调试工具Debug Sampler(转载)
  19. (八) .launch文件 ---编写简单的启动脚本文件
  20. java 注释annotation学习---两篇不错的blog

热门文章

  1. secureCRT背景颜色
  2. BI之路学习笔记1--SSIS包的认识和设计
  3. css的优先级 和 权重问题 以及 !important 优先级
  4. python函数知识三 函数名的使用、格式化、递归
  5. Oracle将两张表的数据插入第三张表且第三张表中不存在
  6. SpringBoot2.x 整合Spring-Session实现Session共享
  7. c++小游戏——俄罗斯方块
  8. C++里long的字节数
  9. Codeforces比赛注意事项(英语比较好,能翻译题目的可以跳过此文章)
  10. Flume+Kafka收集Docker容器内分布式日志应用实践