String library functions
- Summary of commonly used string library functions
-
- Function usage and realization
-
- The purpose and realization of char* strcat( char *s1, char *s2)
- The purpose and realization of char* strchr( char *s1, int ch)
- The purpose and realization of char* strcmp( char *s1, char *s2)
- The purpose and realization of char* strcpy( char *s1, char *s2)
- The purpose and realization of unsigned strlen (char*s)
- The purpose and realization of char* strstr( char *s1, char *s2)
Summary of commonly used string library functions
Function usage and realization
The purpose and realization of char* strcat( char *s1, char *s2)
#include<stdio.h>
#include<string.h>
int main()
{
char arr1[20] ="abcd";
char arr2[20] ="efdg";
char* pr = NULL;
pr = strcat(arr1, arr2); // 返回一个char*指针
printf( "%s\n",pr);
puts(pr);
return 0;
}
note:
- arr1 must specify the size, otherwise the space will be used illegally
- Both printing methods are available
Simulation implementation
#include<stdio.h>
#include<string.h>
char* My_strcat(char* p, char* r)
{
int i = 0;
while (*p != '\0')
{
p++;
i++;
}
int j = 0;
char tmp = 0;
while (*r != '\0')
{
*p = *r;
p++;
r++;
i++; // 计数器i 得知指针p移动的距离 在退回去
}
return p-i;
}
int main()
{
char arr1[20] = "ab cd"; // arr 容量不可少
char arr2[20] = "ef0ko";
char* pr=My_strcat(arr1,arr2);
printf(pr);
return 0;
}
The purpose and realization of char* strchr( char *s1, int ch)
Note: What is returned is the address of the string after ch
#include<stdio.h>
#include<string.h>
int main()
{
char arr[20] = "abcdef";
int ch = 'b';
char* pr = strchr(arr, ch);
printf("%p\n", *pr);
printf(pr);
return 0;
}
What is returned is the address after the
simulation implementation:
char* My_strchr(char* p, int k)
{
while (*p != '\0')
{
if (*p == k)
{
return p;
}
p++;
}
return NULL;
}
int main()
{
char arr[20] = "abcdef";
int ch = 'd';
char * pr = My_strchr(arr, ch);
printf(pr);
return 0;
}
The purpose and realization of char* strcmp( char *s1, char *s2)
#include<stdio.h>
#include<string.h>
int main()
{
char arr[20] = "adc";
char arr2[20] = {
0 };
scanf("%s", arr2);
if (!(strcmp(arr, arr2)))
{
printf("oppen");
}
else
{
printf("no");
}
return 0;
}
实现密码开锁 以及字符串比较
Simulation
method one
int My_strcmp(char* p, char* r)
{
int i = 0;
int j = 0;
int b = 0;
assert((*p != NULL) && (*r != NULL));
while (*(p + i) == '\0')
{
i++;
}
while (*(p + i) == '\0')
{
j++;
}
if (i != j)
{
return i - j; // 不相等返回差值
}
else
{
for (int a = 0; a < i; a++)
{
if (*(p + a) != *(r + a))
{
return 0; // 不相等返回0
}
}
return 1; // 相等返回1
}
}
int main()
{
char arr[20] = "abc";
char arr2[20] = "abcd";
if ((My_strcmp(arr, arr2))==1)
{
printf("oppen");
}
else
{
printf("no");
}
return 0;
}
Method Two
#include<stdio.h>
#include<string.h>
#include<assert.h>
int My_strcmp(const char* p, const char* r)
{
assert ((*p != NULL) && (*r != NULL));
while (*p == *r)
{
if (*p == '\0')
{
return 0;
}
p++;
r++;
}
return *p - *r;
}
int main()
{
char arr[20] = "abc";
char arr2[20] = "abcd";
if ((My_strcmp(arr, arr2))==0)
{
printf("oppen");
}
else
{
printf("no");
}
return 0;
}
The purpose and realization of char* strcpy( char *s1, char *s2)
Copy a string
#include<stdio.h>
#include<string.h>
#include<assert.h>
int main()
{
char arr1[20] = "abcd";
char arr2[20] = {
0 };
char*p=strcpy(arr2, arr1);
//My_strcpy(arr2, arr1);
printf(arr2);
printf("\n");
printf(p);
return 0;
}
note
- Copy the back to the front.
- Pay attention to the allocation of memory, otherwise it will be regarded as illegal memory access
- What is returned is the first address of the copied string.
Simulation implementation
#include<stdio.h>
#include<string.h>
#include<assert.h>
char* My_strcpy(char* r, char* p)
{
int c = 0;
while (*p != '\0')
{
*r = *p;
p++;
r++;
c++;
}
return r - c;
}
int main()
{
char arr1[20] = "abcd";
char arr2[20] = {
0 };
char*p=My_strcpy(arr2, arr1);
//My_strcpy(arr2, arr1);
printf(arr2);
printf("\n");
printf(p);
return 0;
}
The purpose and realization of unsigned strlen (char*s)
First of all, the difference between strlen and sizeof I have written a special blog, poke sizeof and strlen here
Simulate strlen
method 1
#include<stdio.h>
#include<string.h>
#include<assert.h>
int My_strlen(char* p)
{
int i = 0;
while (*p != '\0')
{
p++;
i++;
}
return i;
}
int main()
{
char arr[] = "abcd";
int len = My_strlen(arr);
printf("%d", len);
return 0;
}
Method 2 recursion
#include<stdio.h>
#include<string.h>
#include<assert.h>
int My_strlen(char* p)
{
if (*p == '\0')
{
return 0; // 出口
}
return 1+My_strlen(p+1);
}
int main()
{
char arr[] = "abcd";
int len = My_strlen(arr);
printf("%d", len);
return 0;
}
The purpose and realization of char* strstr( char *s1, char *s2)
#include<stdio.h>
#include<string.h>
#include<assert.h>
int main()
{
char arr1[20] = "abcdefg";
char arr2[20] = "def";
char* p = strstr(arr1, arr2);
printf(p);
return 0;
}
Simulation implementation
#include<stdio.h>
#include<string.h>
#include<assert.h>
char* My_strstr(char* p, char* r)
{
int i = 0;
int k = 0;
while (*p != '\0')
{
if (*p == *r)
{
r++;
i++;
p++;
}
else if (*r == '\0')
{
return p - k;
}
else
{
p++;
k++;
}
}
if (*p == '\0')
{
return p - i;
}
return NULL;
}
int main()
{
char arr1[20] = "abcdefg";
char arr2[20] = "bcdefg";
char* p = My_strstr(arr1, arr2);
printf(p);
if (p == NULL)
{
printf("heh");
}
return 0;
}