70,023
社区成员




unsigned int strlenth(char *s)
{
unsigned int lenth = 0;
if(!s){
return 0;
}
while(*(s++)){
lenth++;
}
return lenth;
}
void strcopy(char **target, char *source)
{
unsigned length = strlenth(source);
*target = (char*)malloc(length);
**target = 0;
if(!source || !*target){
return;
}
unsigned int i = 0;
while(source[i]){
(*target)[i] = source[i++];
}
(*target)[i] = '\0';
}
int strcompare(char *s, char *t)
{
if(!s && !t){
return 0;
}
if(s && !t){
return 1;
}
if(!s && t){
return -1;
}
while(*s || *t){
if(*s > *t){
return 1;
}else if(*s < *t){
return -1;
}
s++;
t++;
}
return 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);
**x = '\0';
unsigned int i = 0;
while(*s){
(*x)[i++] = *(s++);
}
while(*t){
(*x)[i++] = *(t++);
}
(*x)[i] = '\0';
}
void strcatch(char *s, unsigned int index, unsigned int lenth, char **t)
{
unsigned int length = strlenth(s);
if(!s || lenth > length - index || lenth < 1 || index > length){
return;
}
*t = (char*)malloc(lenth);
**t = 0;
unsigned int i = index, j= 0;
for(; i <index + lenth; i++){
(*t)[j++] = s[i];
}
(*t)[j] = '\0';
}
bool strsubstr(char *s, char *sub)
{
bool result = 0;
if(!s || !sub){
return 0;
}
unsigned int i = 0, j = 0;
unsigned int length1 = strlenth(s);
unsigned int length2 = strlenth(sub);
if(length1 == 0 && length2 == 0){
return 1;
}
for(; i < length1; i++){
if(s[i] == sub[j++]){
if(j == length2){
result = 1;
break;
}
continue;
}else{
j = 0;
}
}
return result;
}
int strcompare(char *s, char *t)
{
if(!s && !t){
return 0;
}
if(s && !t){
return 1;
}
if(!s && t){
return -1;
}
while(*s || *t){ //这个条件是错误滴,*s && *t
if(*s > *t){
return 1;
}else if(*s < *t){
return -1;
}
s++;
t++;
}
//可不能直接返回0
if (*s)
return 1;
else if (*t)
return -1;
else
return 0;
}
void strcopy(char **target, char *source)
{
if(!source){
return;
}
unsigned length = strlenth(source);
*target = (char*)malloc(length + 1); //这里要改
if(*target){
return;
}
unsigned int i = 0;
for(; i<length; ++i){
(*target)[i] = source[i];
}
(*target)[i] = '\0';
}
先修正一个在看下面的