对字符串操作的系统函数的原型实现

 

1.strcmp函数:

int mystrcmp(const char *str1,const char *str2)
{
while((*str1==*str2)&&(*str1))
{
str1++;
str2++;
}
if((*str1==*str2)&&(!*str1)) //Same strings
return 0;
else if((*str1)&&(!*str2))  //Same but str1 longer
return 1;
else if ((!*str1)&&(*str2))
return -1 ;
else
return ((*str1>*str2)?1:-1);

}

2、strcat函数:

char *mystrcat(char *target,const char *source)
{
char *original=target;
while(*target) target++;     // Find the end of the string
while(*target++=*source++);
return(original);
}


int main(void)
{

  char destination[25];
  char * cc;                                                            
char *blank = " ", *c = "C++", *Borland = "Borland";
strcpy(destination, Borland);
mystrcat(destination, blank);
cc=mystrcat(destination, c);
printf("%s\n", cc);

}
3、strcpy函数

char *mystrcpy(char *destination, const char *source)
{
while(*destination++=*source++);            
return (destination);
}

4、strlen函数
int mystrlen(const char *string)
{
int i=0;
while(*string++)
 i++;
 return i;
}
5、streql函数
 int mystreql(char *str1,char *str2)
{
while((*str1==*str2)&&(*str1))
{
str1++;
str2++;
}
return((*str1==NULL)&&(*str2==NULL));
}
6、strchr函数
char * mystrchr(const char *string,char letter)
{
while((*string!=letter)&&(*string))
string++;
return (string);
}

int main(void)
{
char string[15];
char *ptr, c = 'r';
strcpy(string, "This is a string");
ptr = mystrchr(string, c);

if (ptr)
printf("The character %c is at position: %d\n", c,ptr-string);
else
printf("The character was not found\n");
return 0;
}
7、ctrcnt函数
int mychrcnt(const char *string,char letter) /*指定计算字符串内的指定字符的个数*/
{
int count=0;

while(*string)
{
    if(*string==letter)
    count++;
    string++;
}
return count;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值