1. strlen(),计算字符串长度
int strlen (const char *s)
{
const char *p;
for (p = s; *p; ++p);
return p - s;
}
2. strcpy(), 字符串拷贝.
char *stpcpy (char *dst, const char *src)
{
while ((*dst++ = *src++) != '\0');
return dst - 1;
}3. strcat(), 字符串的连接.
char *strcat (char *dst, const char *src)
{
char *ret = dst;
while (*dst++ != '\0');
--dst;
while ((*dst++ = *src++) != '\0');
return ret;
}
4. streql(), 判断两个字符串是否相等.
int streql(char *str1,char *str2)
{
while((*str1==*str2)&&(*str1))
{
str1++;
str2++;
}
return((*str1==NULL)&&(*str2==NULL));
}
5. strchr(), 在字符串中查找某个字符.
char *strchr (const char *s, int c)
{
for (; *s != (char) c; ++s)
if (*s == '\0')
return NULL;
return (char *) s;
}
6. chrcnt(), 计算某个字符在字符串中出现的次数.
int chrcnt(const char *string,int letter)
{
int count=0;
while(*string)
{
if(*string==letter)
count++;
string++;
}
return count;
}
7. strcmp(), 判断两个字符串是否相等.
int strcmp (const char *s1, const char *s2)
{
int ret;
while ((ret = *(unsigned char *) s1 - *(unsigned char *) s2++) == 0
&& *s1++);
return ret;
}
阅读(1297) | 评论(0) | 转发(0) |