1.字符串拷贝
char *mystrcpy(char *dest, const char *src)
{
char *tmp = dest;
while((*dest++ = *src++) != ‘\0’);
return tmp;
}
2.字符串连接
char *mystrcat(char *dest, const char *src)
{
char *tmp = dest;
while(*tmp)
tmp++;
while((*tmp++ = *src++) != '\0');
return dest;
}
3.字符串比较
int mystrcmp(const char *cs, const char *ct)
{
int ret;
while((ret = *cs - *cs++) == 0 && *cs++);
return ret;
}
4.查找字符串中字符
char *mystrchr(char *str, char s)
{
while((*str != '\0') && (*str != s) )
str++;
if(*str == s)
return str;
return NULL;
}
5.查找字符串中字符出现的个数
int mystrcnt(const char *string, char letter)
{
int count = 0;
while(*string)
{
if(*string == letter)
count++;
string++;
}
return count;
}
6.计算字符串长度
int mystrlen(const char *s)
{
int len = 0;
while(*s++ != '\0')
{
len++;
}
return len;
}
7.字符串转换大写字母函数
char *mystrupr(char *str)
{
char *s = str;
do
{
if(*s >= 'a' && *s <= 'z')
*s -= 32;
}while(*s++ != '\0');
}
8.字符串转换小写字母函数
char *mystrlwr(char *str)
{
char *s = str;
do
{
if(*s >= 'A' && *s <= 'Z')
*s += 32;
}while(*s++ != '\0');
}
9.字符串翻转
char *mystrrev(char *src, char *dst)
{
int len = mystrlen(src);
char *p = src+len-1;
char *q = dst;
while(len--)
*(q++) = *(p--);
*(q++) = '\0';
return dst;
}
10.在一字符串中查找指定字符串
const char *mystrstr(const char *find, const char *need)
{
int i = 0, j = 0, tmp;
for(i = 0; find[i] != '\0'; i++)
{
tmp = i;
j = 0;
while(find[i++] == need[j++])
{
if(need[j] == '\0')
return &find[tmp];
}
i = tmp;
}
return NULL;
}