OAIP/21-22/22.c

99 lines
1.9 KiB
C
Raw Permalink Normal View History

#include "stdio.h"
#include "string.h"
#include "Windows.h"
int mystrlen(char[]), mystrlenpoint(char*);
int mystrcmp(char[], char[]), mystrcmppoint(char*, char*);
void mystrcpy(char*, char*);
void mystrcat(char*, char*);
void mystrcpyn(char*, char*, int);
int main() {
char str[100];
char str1[100];
int n;
printf("cpyn\n");
scanf_s(" %[^\n]", &str1, 100);
scanf_s("%d", &n);
mystrcpyn(&str, &str1, n);
printf("1 = %s, 2 = %s\n", str, str1);
printf("len\n");
scanf_s(" %[^\n]", &str, 100);
printf("len = %d\n", mystrlen(str));
printf("len pointer\n");
scanf_s(" %[^\n]", &str, 100);
printf("len pointer = %d\n", mystrlenpoint(&str));
printf("cmp\n");
scanf_s(" %[^\n]", &str, 100);
scanf_s(" %[^\n]", &str1, 100);
printf("str - str1 = %d\n", mystrcmp(str, str1));
printf("cmp pointer\n");
scanf_s(" %[^\n]", &str, 100);
scanf_s(" %[^\n]", &str1, 100);
printf("str - str1 = %d\n", mystrcmppoint(&str, &str1));
printf("cpy\n");
scanf_s(" %[^\n]", &str1, 100);
mystrcpy(str, str1);
printf("1 = %s, 2 = %s\n", str, str1);
printf("cat\n");
scanf_s(" %[^\n]", &str, 100);
scanf_s(" %[^\n]", &str1, 100);
mystrcat(str, str1);
printf("%s", str);
return 0;
}
int mystrlen(char c[]) {
int k = 0;
for (int i = 0; c[i] != '\0'; i++) k++;
return k;
}
int mystrlenpoint(char *c) {
char* p = c;
while (*p++);
return p - c - 1;
}
int mystrcmp(char f[], char s[]) {
int i;
for (i = 0; f[i] != '\0' && s[i] != "\0"; i++) {
if (f[i] - s[i] != 0) break;
}
return f[i] - s[i];
}
int mystrcmppoint(char *f, char *s) {
int i;
for (i = 0; f[i] != '\0' && s[i] != "\0"; i++) {
if (f[i] - s[i] != 0) break;
}
return f[i] - s[i];
}
void mystrcpy(char *f, char *s) {
while (*f++ = *s++);
}
void mystrcat(char* f, char* s) {
while (*f) *f++;
while (*f++ = *s++);
}
void mystrcpyn(char* f, char* s, int n) {
int i = 0;
for (i = 0; s[i] != '\0' && i < n; i++) {
f[i] = s[i];
}
f[i] = '\0';
}