版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lu_LLLR/article/details/79976803
串是由零个或者多个
字符组成的
有限序列,串中字符的个数称为
串的长度,含有零个元素的串叫
空串。
串通常用一个
字符数组来表示。串中
任意连续的字符组成的子序列称为该串的
子串,包含子串的串称为
主串,某个字符在串中的序号称为这个字符的位置。通常
用子串第一个字符的位置作为子串在主串中的位置。
空格也是串字符集合的一个元素,由一个或多个空格组成的串称为空格串(空格串不是空串)串的逻辑结构和线性表类似,串是限定了元素为字符的线性表。在操作上,串和线性表有很大区别,线性表的操作主要针对表内的某一个元素,而串操作主要针对串内的一个子串。
-------------------------------------------------------------------------------------------------------------------------------
由于串的定长顺序存储不怎么用,就不写了。
本篇写顺序存储之串的堆分配存储表示。
串的一些操作有以下几个注意点:
1、赋值操作不能直接用“=”,串是一个数组,将一个数组赋值给另一个数组直接用“=”是不行的,必须对数组内的每个元素进行逐一赋值操作
2、串的比较操作是串排序应用中的核心操作。设两串A和B中的待比较字符分别是a和b,如果a的ASCII码小于b的ASCII码,则返回A小于B标记;若反之,则返回A大于B标记,在没有比较出A和B大小的情况下,先结束的串为较小串
串的堆分配存储:
typedef struct
{
char *ch; //指向动态分配存储区首地址的字符指针
int length; //串长度
}HString;
接下来就直接上代码吧,具体的都写在注释里了
#include <stdio.h>
#include <malloc.h>
#include <string.h>
//串的顺序存储
//堆分配存储
typedef struct
{
char *ch; //指向动态分配存储区首地址的字符指针
int length; //串长度
}HString;
/**************初始化***************/
void InitString(HString *s)
{
s->ch = NULL;
s->length = 0;
}
/***************字符串赋值*************/
void StrAssign(HString *s, char *ch)
{
int len = strlen(ch); //字符串的长度
//char *c = ch;
//判断字符串是否为空,不为空则释放
if (s->ch)
free(s->ch);
//判断赋值的内容是否为空,为空则不赋值
if (!len)
{
s->ch = NULL;
s->length = 0;
}
//根据串的长度向内存申请空间,赋值给串s
else
{
s->ch = (char*)malloc(sizeof(char)*len);
if (s->ch == NULL)
return 0;
else
{
for (int i = 0; i < len; ++i)
{
s->ch[i] = ch[i];
//printf("%c", s->ch[i]);
}
s->length = len;
return 1;
}
}
}
/******************打印字符串******************/
void printString(HString s)
{
for (int i = 0; i < s.length; i++)
printf("%c", s.ch[i]);
printf("\n");
}
/*****************取串长度**************/
int strlength(HString s)
{
printf("串的长度是%d\n",s.length);
}
/***************串比较操作**************/
int strcompare(HString s1, HString s2)
{
for (int i = 0; i < s1.length && i < s2.length; i++)
{
if (s1.ch[i] != s2.ch[i])
printf("%d\n", s1.ch[i] - s2.ch[i]);
else
printf("%d\n",s1.length - s2.length);
}
}
/*******************串连接操作******************/
int concatString(HString *str,HString s1, HString s2)
{
if (str->ch)
{
free(str->ch);
str->ch = NULL;
}
str->ch = (char*)malloc(sizeof(char)*(s1.length + s2.length + 1));
if (str->ch == NULL)
return 0;
int i = 0;
while (i < s1.length)
{
str->ch[i] = s1.ch[i];
++i;
}
int j = 0;
while (j <= s2.length) //用“<=”是为了连同最后的'\0'一起复制
{
str->ch[i + j] = s2.ch[j];
++j;
}
str->length = s1.length + s2.length;
return 1;
}
/*****************求子串操作******************/
//求str串中从pos位置开始,长度为len的子串
int subString(HString *substr, HString str, int pos, int len)
{
if (pos < 0 || pos >= str.length || len<0 || len>str.length - pos)
return 0;
if (substr->ch)
{
free(substr->ch);
substr->ch = NULL;
}
if (len == 0)
{
substr->ch = NULL;
substr->length = 0;
return 1;
}
else
{
substr->ch = (char*)malloc(sizeof(char)*len + 1);
int i = pos;
int j = 0;
while (i < pos + len)
{
substr->ch[j] = str.ch[i];
++i;
++j;
}
substr->ch[j] = '\0'; //给个位置给'\0'
substr->length = len;
return 1;
}
}
/***************串清空操作****************/
int clearString(HString *str)
{
if (str->ch)
{
free(str->ch);
str->ch = NULL;
}
str->length = 0;
return 1;
}
int main()
{
HString str,str1,s1,s2;
InitString(&s1);
StrAssign(&s1,"input");
printString(s1);
strlength(s1);
InitString(&s2);
StrAssign(&s2, "put");
printString(s2);
strlength(s2);
strcompare(s1, s2);
InitString(&str);
concatString(&str, s1, s2);
printString(str);
InitString(&str1);
subString(&str1, s1, 2, 2);
printString(str1);
clearString(&str1);
strlength(str1);
system("pause");
}