使用指针完成。
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define maxn 512
void mystrcpy(char *p,char *q) {
while (*q != '\0') {
*p = *q;
p++;
q++;
}
}
int mystrcmp(char *p, char *q) {
while (*p == *q) {
if (*p == '\0') {
return 0;
}
p++;
q++;
}
if (*p > *q) {
return 1;
}
else {
return -1;
}
}
char *mystrcat(char *p, char *q) {
char *temp = p;
while (*p != '\0') {
p++;
}
while (*q != '\0') {
*p = *q;
p++;
q++;
}
return temp;
}
int mystrlen(char *p) {
int count = 0;
while (*p != '\0') {
p++;
count++;
}
return count;
}
int main() {
char a[maxn] = "hello world", b[maxn] = {0};
strcpy(b,a);
printf("%s\n", b);
mystrcpy(b,a);
printf("%s\n",b);
char c[maxn] = "hello";
printf("%d\n", strcmp(a, c));
printf("%d\n",mystrcmp(a, c));
printf("%s\n", mystrcat(a, c));
printf("%d\n", strlen(a));
printf("%d\n", mystrlen(a));
return 0;
}