C语言实现串,和简单的主函数测试

本文档展示了如何使用C语言实现基本的字符串操作,包括构造字符串、获取字符串长度、字符串比较、清除字符串、字符串拼接、提取子串以及输出字符串。通过一个主函数测试了这些操作的有效性。
摘要由CSDN通过智能技术生成
#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 &
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值