-
字符串操作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;
}