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;
}