OJ 系列之字符串基本操作

本文介绍了一组不依赖标准库的C语言字符串操作函数,包括计算字符串长度、字符串拷贝、比较、连接、截取及子串查找等功能,并提供详细的实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  • 字符串操作C语言提供了较多的库函数,本题目要求代码中不能使用字符串操作相关的库函数,可以使用malloc。

  • 用例中可以使用中提供的库函数。

  • 实现接口,每个接口实现1个基本操作:

  • unsigned int strlenth(char*s):计算字符串的长度。

  • void strcopy(char**target,char*source):字符串拷贝,从source拷贝到target中。

  • int strcompare(char*s,char*t):字符串比较,比较的原则:

对两个字符串的每个字符从左到右逐个比较,进行ASCⅡ比较,只要一个字符大,则整个字符大;

如果每个字符相等,但是其中一个字符串长度更长,则此字符串更大。

如果s>t,返回1;如果s==t,返回0;如果s<t,返回-1。<>

  • void strcombine(char**x,char*s,char*t):字符串连接,将字符串t接到s后面,并存储到x中。

  • void strcatch(char*s,unsignedintindex,unsignedintlenth,char**t):字符串截取,对字符串s中,从第index个字符开始,截取lenth长度的字符串,并把截取出来的字符串存储到t中。

  • bool strsubstr(char*s,char*sub):子串查找,在字符串s中查找字符串sub是否存在,如果存在则返回1,否则返回0。

题目框架中有17个参考用例,其它用例请执行编写。

/******************************************************************************

  Copyright (C), scut.

 ******************************************************************************
  File Name     : 
  Version       : 
  Author        : ZP1015
  Created       : 2016/01/15
  Last Modified :
  Description   : 
  Function List :
              
  History       :
  1.Date        : 2016/01/15
    Author      : ZP1015
    Modification: Created file

******************************************************************************/
#include <stdlib.h>

unsigned int strlenth(char *s)  /* 获取字符串长度 */
{
	/* 1.异常处理 */
	if(NULL == s)
		return 0;
	
	unsigned int lenth = 0;
	
	while(*s != '\0') {
		lenth ++;
		s++ ;
	}

	return lenth;
}

void strcopy(char **target, char *source)  /* 字符串拷贝 */
{
	/* 1.异常处理 */
	unsigned int length = strlenth(source);
    *target = (char*)malloc(length+1);
    **target = 0;
	
    if(!source || !*target){
        return;
    }
    unsigned int i = 0;
 
    while(source[i]){
        (*target)[i] = source[i++];
    }
    (*target)[i] = '\0';
}

/* 字符串比较,s>t,则返回1;s=t,则返回0;s<t,则返回-1 */
int strcompare(char *s, char *t)  
{
	/*1.异常处理 */
	if(!s && !t) /*都为空字符串*/
		return 0;
	if (!s && t)
		return -1;
	else if(!t && s)
		return 1;
	
	while(*s !='\0'&&*t !='\0') {
		if(*s >*t) 
			return 1;
		else if(*s <*t)
			return -1;		
		t++;
		s++;
	}
	if(*s == '\0'&&*t=='\0')
		return 0;
	else if(*s =='\0')
		return -1;

	
	return 1; /**t == '\0'*/
}

/* 字符串连接,将字符串t接到s后面,x为连接后的新串 */
void strcombine(char **x, char *s, char *t) 
{
	/* 1.异常处理 */
	unsigned int length = strlenth(s)+strlenth(t);
	*x = (char *)malloc(length+1);
	
	if(!x||!*x)
		return;
	
	unsigned int i = 0;
	unsigned int j = 0;
	
	while(!s && s[i]) {
		(*x)[i] = s[i];
		i ++;
	}
	j = i;
	i = 0;
	while(!t && t[i]) {
		(*x)[j] = t[i];
		i ++;
		j ++;
	}
	
	(*x)[j] = '\0';
		
}

void strcombine(char **x, char *s, char *t)  
{
	
	if(!s){
		*s = '\0';
	}
	if(!t){
		*t = '\0';
	}
	unsigned int length = strlenth(s) + strlenth(t);
	*x = (char*)malloc(length + 1);
	**x = '\0';
	unsigned int i = 0;
	while(*s){
		(*x)[i++] = *(s++);
	}
	while(*t){
		(*x)[i++] = *(t++);
	}
	(*x)[i] = '\0';

}

/* 字符串截取,从第index个字符开始,截取lenth长度的字符串,并输出到字符串t */
void strcatch(char *s, unsigned int index, unsigned int lenth, char **t)  
{
	/* 1.异常处理 */
	unsigned int length = strlenth(s);
	if(lenth > length - index || lenth < 1 || index > length){
		return;
	}
	
	*t = (char *)malloc(lenth+1);
	**t = 0;
	
	if(!s||!t||!*t)
		return;
	
	unsigned int i = index, j= 0;
	
	for(; i <index + lenth; i++) {
		(*t)[j++] = s[i];
	}
	
	(*t)[j] = '\0';	
}

/* 字符串子串查找,如果子串sub在s中存在,则返回1,否则返回0 */
bool strsubstr(char *s, char *sub) 
{
	/* 1.异常处理 */
	bool result = 0;
	if(!s ||!sub)
		return 0;
	unsigned int sLen =  strlenth(s);
	unsigned int subLen =  strlenth(sub);
	
	if(sLen == 0 && subLen == 0)
		return 1;
	
	unsigned int i= 0,j=0;
	
	for(i = 0;i < sLen;i++) {
		if(s[i] == sub[j++]) {
			if(j == subLen) {
				return 1;
			}
			continue;	
		} else {
			j = 0;
		}
		
	}

	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

狂奔的乌龟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值