#include<stdio.h>
#include<stdlib.h>
#define OK 1
#define FALSE 0
#define Status int
typedef struct{
char *ch;
int length;
}HString;
//构造一个串T和chars相同
Status StrAssign (HString **T ,char *chars)
{
int len,j;
char *c;
if((*T)->ch)
free((*T)->ch);
else
{
for(len = 0,c=chars; *c;c++,len++);
if(!len)
fprintf(stderr,"chars is empty\n");
(*T)->ch = (char *)malloc(sizeof(char) * len);
if(!(*T)->ch)
fprintf(stderr,"error\n");
for(j=0;j<len;j++)
{
(*T)->ch[j] = chars[j];
(*T)->length++;
}
}
return OK;
}
//返回串的元素个数(长度)
Status StrLength(HString *T)
{
char *c;
int len;
if(!T->ch)
fprintf(stderr,"empty error\n");
else
{
for(len=0,c=T->ch;*c;len++,c++);
}
return len;
}
//比较串s,和串T
Status StrCompare(HString *S,HString *T)
{
int i;
for(i=0; i<S->length &
C语言实现串,和简单的主函数测试
最新推荐文章于 2024-09-30 16:38:03 发布
本文档展示了如何使用C语言实现基本的字符串操作,包括构造字符串、获取字符串长度、字符串比较、清除字符串、字符串拼接、提取子串以及输出字符串。通过一个主函数测试了这些操作的有效性。
摘要由CSDN通过智能技术生成