标签:sre get strong == main oid str1 char s gets
分别模拟实现库函数1.strcat
#include<stdio.h>
#include<string.h>
void Merge(char a[], char b[], int n)
{
char* q = a;
char* p = b;
int m = 0;
while (*q != ‘\0‘)
q++;
while (m <= n) {
*q++ = *p++;
m++;
}
puts(a);
}
int main()
{
char a[100] = { 0 };
char b[100] = { 0 };
gets(a);
gets(b);
int n = strlen(b);
Merge(a, b, n);
}
2.strcmp
#include <stdio.h>
#include <assert.h>
int Strcmp(const char* str1, const char* str2) {
assert(str1 != NULL);
assert(str2 != NULL);
while (*str1 == *str2) {
if (*str1 == ‘\0‘) {
return 0;
}
++str1;
++str2;
}
return *str1 - *str2;
}
int main() {
char str1[] = "love";
char str2[] = "move";
int point = Strcmp(str1, str2);
if (point == 0) {
printf("str1 = str2\n");
}
else if (point > 0) {
printf("str1 > str2\n");
}
else {
printf("str1 < str2\n");
}
return 0;
}
3.strcpy
#include<stdio.h>
#include<string.h>
void my_strcpy(char *arr_1, char *arr_2)
{
while (*arr_2!=0) {
*arr_1++ = *arr_2++;
}
}
int main()
{
char arr_1[100] = { 0 };
char arr_2[100] = { 0 };
gets_s(arr_1);
/*int a = strlen(arr_1);*/
gets_s(arr_2);
my_strcpy(arr_1, arr_2);
printf("%s", arr_1);
}
4.srelen
#include<stdio.h>
void my_strlen(char* arr)
{
int count = 0;
while (*arr != 0) {
arr++;
count++;
}
printf("%d", count);
}
int main()
{
char arr[100] = { 0 };
gets_s(arr);
my_strlen(arr);
}
模拟实现 strcat,strcmp,strcpy,srelen
标签:sre get strong == main oid str1 char s gets
原文地址:https://blog.51cto.com/14946388/2551657