strlen
用法:计算字符串里的字符个数(不加'\0')
1字符串以’\0为结尾‘
2返回值为size_t(无符号)
实现strlen
#include<stdio.h>
int mystrlen(const char* str)
{
int count = 0;
while (*str++)
count++;
return count;
}
int main()
{
int len = mystrlen("abcdef");
printf("%d", len);
return 0;
}
strcpy
用途:将后方的字符串拷贝到前面的字符串
1字符串都是以‘\0’为结尾
2目标空间足够大
3目标空间可以改变
实现strcpy
#include<stdio.h>
char* mystrcpy(char* dest,const char* str)
{
char* ret = dest;
while (*dest++= *str++);
return ret;
}
int main()
{
char ch1[10] = "abcde";
char ch2[] = "qweaaa";
mystrcpy(ch1, ch2);
printf("%s", ch1);
return 0;
}
strncpy
用途:将后方的n个字符串拷贝到前面的字符串,若字符串不够补0.
1字符串都是以‘\0‘为结尾
2目标空间足够大
3目标空间可以改变
实现
#include<stdio.h>
char* mystrcpy(char* dest, const char* str,size_t count)
{
char* ret = dest;
while (count && (*dest++ = *str++))
count--;
if (count)
while (--count)
*dest = '\0';
return ret;
}
int main()
{
char ch1[10] = "abcde";
char ch2[] = "qweaaa";
mystrcpy(ch1, ch2,3);
printf("%s", ch1);
return 0;
}
strcat
用途:将后面的字符串连接到前面的字符串
1字符串都是以0为结尾
2目标空间足够大
3目标空间可以改变
实现strcat
#include<stdio.h>
char* mystrcat(char* dest,const char* str)
{
char* ret = dest;
while (*dest != 0)
dest++;
while (*dest++ = *str++);
return ret;
}
int main()
{
char ch1[20] = "hello ";
char ch2[] = "world";
mystrcat(ch1, ch2);
printf("%s", ch1);
return 0;
}
strncat
用途:将后面的n个字符串连接到前面的字符串
实现:
#include<stdio.h>
char* mystrcat(char* dest, const char* str,size_t count)
{
char* ret = dest;
while (*dest != 0)
dest++;
while (count-- && (*dest++ = *str++) && str);
return ret;
}
int main()
{
char ch1[20] = "hello ";
char ch2[] = "world";
mystrcat(ch1, ch2,3);
printf("%s", ch1);
return 0;
}
strcmp
用途:比较字符串大小
规则:若第一个字符串大于第二个字符串,返回值>0,若小于,返回值<0
若等于,返回值=0。
实现
#include<stdio.h>
size_t mystrcmp(const char* dest,const char* str)
{
while (*dest++ == *str++ && *str != 0 && *dest != 0);
if (*dest-- > *str--)
return 1;
else if (*dest == *str)
return 0;
else
return -1;
}
int main()
{
const char* p1 = "hello";
const char* p2 = "hello";
size_t ret = mystrcmp(p1, p2);
printf("%d", ret);
return 0;
}
strncmp
用途:比较钱n个字符串大小
实现
#include<stdio.h>
size_t mystrcmp(const char* dest, const char* str,size_t count)
{
while (count-- && (*dest++ == *str++) && *str && *dest);
if (*dest-- > *str--)
return 1;
else if (*dest == *str)
return 0;
else
return -1;
}
int main()
{
const char* p1 = "hello";
const char* p2 = "hello";
size_t ret = mystrcmp(p1, p2,8);
printf("%d", ret);
return 0;
}
strstr
用途:在前一个字符串查找后一个字符串
实现:
#include<stdio.h>
char* mystrstr(const char*p1,const char*p2)
{
char* s1; char* s2;
char* cur = (char*)p1;
if (*p2 == NULL)
return (char*)p1;
while (*cur)
{
s1 = cur;
s2 = (char*)p2;
while ((*s1 == *s2) && *s1 && *s2)
s1++,s2++;
if (*s2 == 0)
return cur;
cur++;
}
return NULL;
}
int main()
{
const char* p1 = "hello";
const char* p2 = "e";
char*x =mystrstr(p1, p2);
printf("%s",x);
return 0;
}
strtok
用途:以后面的字符为分割点,找到前面的字符之前的部分。
1会改变字符串。
2将字符改成NULL
#include<stdio.h>
#include<string.h>
int main()
{
char arr[30] = "123.456@789";
const char* p = "@.";
char* ret = NULL;
for(ret = strtok(arr, p);ret!=NULL;ret=strtok(NULL,p))
printf("%s", ret);
return 0;
}
strerror(errno)
用途:C语言库里发生错误,返回到errno中
各种函数
tolower将大写转小写
toupper小写转大写
标签:return,函数,dest,char,++,str,字符串,const From: https://blog.51cto.com/u_15770447/5882673