字符串查找
strchr
strrchr
strpbrk
strstr
高级字符串查找
strspn
strcspn
#include
#include
int my_strlen(const char *str) //const在这里修饰是一大亮点 因为strlen函数只需计算字符串的长度,
{ //无需更改,所以加上const修饰,不能随意改变
int count = 0;
assert(str != NULL); // assert()在这里是第二亮点,断言的作用,判断指针是否为空指针
while(*str != '\0')
{
count++;
str++;
}
return count;
}
int main()
{
char *p = "abcdef";
int ch = my_strlen(p);
printf("%d\n",ch);
return 0;
}
#include
#include
#include
char * my_strcpy(char * str_des, const char * str_source)
{
assert(str_des);
assert(str_source);
char * ret = str_des;
while (*ret++ = *str_source++)
{
;
}
return str_des;
}
int main()
{
char str1[] = "hello world";
char str2[20] = { 0 }; //将空间全部初始化为0
char *str3 = my_strcpy(str2, str1);
printf("%s\n", str2);
printf("%s\n", str3);
system("pause");
return 0;
}
#include
#include <assert.h>
#include
char * my_strcat(char * str_source, char * str_des)
{
assert(str_des);
assert(str_source);
char *p = str_source;
while (*p) //不能写成*p++当p开始为0还会++
{
p++;
}
while (*p++ = *str_des++);
return str_des;
}
int main()
{
char str[40] = { 0 };
my_strcat(str,"hello ");
printf("%s\n",str);
my_strcat(str,"world!");
printf("%s\n", str);
system("pause");
return 0;
}
#include
#include
#include
//strcmp字符串比较
//两个字符串相等返回0,第一个大于第二个返回正数,反之负数
int my_strcmp(const char* str1, const char * str2)
{
while (*str1 && *str2)
{
if (*str1 == *str2)
{
str1++;
str2++;
}
else if (*str1 > *str2)
return 1;
else
return -1;
}
if (*str1)
return 1;
if (*str2)
return -1;
return 0;
}
int main()
{
char str1[] = "a";
char str2[] = "b";
int n = my_strcmp(str1, str2);
printf("%d\n", n);
system("pause");
return 0;
}
#include
#include
#include
char * my_strncpy(char * str_des, const char * str_source,int size)
{
assert(str_des);
assert(str_source);
char * ret = str_des; //定义一个指针要拷贝的空间首地址
while (size--)
{
*ret++ = *str_source++;
}
return str_des;
}
int main()
{
char str1[] = "hello world";
char str2[20] = { 0 }; //将空间全部初始化为0
char *str3 = my_strncpy(str2, str1,4);
printf("%s\n", str2);
printf("%s\n", str3);
system("pause");
return 0;
}
#include
#include <assert.h>
#include
char * my_strncat(char * str_source, char * str_des,int size)
{
assert(str_des);
assert(str_source);
char *p = str_source;
while (*p) //不能写成*p++当p开始为0还会++
{
p++;
}
while (size--)
{
*p++ = *str_des++;
}
return str_des;
}
int main()
{
char str[40] = { 0 };
my_strncat(str, "hello ",3);
printf("%s\n", str);
my_strcat(str, "world!",2);
printf("%s\n", str);
system("pause");
return 0;
}
#include
#include
#include
int my_strncmp(const char* str1, const char * str2,int size)
{
while (size--)
{
if (*str1 == *str2)
{
str1++;
str2++;
}
else if (*str1 > *str2)
return 1;
else
return -1;
}
return 0;
}
int main()
{
char str1[] = "abcd";
char str2[] = "abce";
int n = my_strncmp(str1, str2,3);
printf("%d\n", n);
system("pause");
return 0;
}
#include
#include
#include
//找一个字符在一个字符串出现的位置
char * my_strchr(const char *str, char p)
{
while (*str)
{
if (*str == p)
return str;
str++;
}
return NULL;
}
int main()
{
char str[] = "abcd";
char * p = my_strchr(str,'x');
printf("%s\n", p);
system("pause");
return 0;
}
#include
#include
#include
char * my_strrchr(char * str, char x)
{
char *p = str + strlen(str) - 1;
while (p != str-1)
{
if (*p == x)
return p;
p--;
}
return NULL;
}
int main()
{
char str[] = "This is a sample string ";
char * ret = my_strrchr(str,'t');
printf("%s\n", ret);
system("pause");
return 0;
}
#include
#include
#include
char * my_strpbrk(char * str,char* key)
{
char *s = str;
char *k = key;
while(*s)
{
while (*k)
{
if (*s == *k)
return s;
k++;
}
s++;
k = key;
}
return NULL;
}
int main()
{
char str[] = "This is a sample string ";
char key[] = "aeiou";
char *pch = my_strpbrk(str,key);
while (pch != NULL)
{
printf("%c ", *pch);
pch = my_strpbrk(pch+1, key);
}
system("pause");
return 0;
}
在字符串str1中找出str2子串,返回str1中str2的位置
#include
#include
#include
strstr在字符串str1中找出str2子串,返回str1中str2的位置
char* my_strstr(char * str1, char * str2)
{
char *pa1 = str1;
char *pb1 = pa1;
char *pa2 = str2;
if (str2 == NULL)
return NULL;
while (*pa1)
{
pb1 = pa1;
pa2 = str2;
while (*pb1 && *pa2 && *pb1 == *pa2)
{
pb1++;
pa2++;
}
if ('\0' == *pa2)
return pa1;
pa1++;
}
return NULL;
}
int main()
{
char str1[] = "123abc889";
char str2[] = "7";
char *p = my_strstr(str1, str2);
printf("%s\n", p);
system("pause");
return 0;
}
int my_strspn(char * str1, char * str2)
{
int count = 0;
char * p2 = str2;
while (*str1)
{
while (*p2)
{
if (*str1 == *p2)
{
count++;
break;
}
p2++;
}
p2 = str2;
str1++;
}
return count;
}
int main()
{
char str1[] = "23d";
char str2[] = "1234abcd";
int n= my_strspn(str1, str2);
printf("%d\n", n);
system("pause");
return 0;
}
strcspn函数 遍历str1中的字符,直到遇到str2中的字符停下来,返回str1遇到之前遍历字符数
可以使用strcspn函数,返回str2中字符在str1中第一次出现的地址,用该地址减去str2的地址就是
遍历的字符数
int my_strcspn(char * str1, char *str2)
{
char *p = strpbrk(str1, str2);
return p - str1;
}
int main()
{
char str1[] = "123abc";
char str2[] = "7a";
int n = my_strcspn(str1, str2);
printf("%d\n", n);
system("pause");
return 0;
}