77 lines
1.5 KiB
Plaintext
77 lines
1.5 KiB
Plaintext
|
#include <Windows.h>
|
|||
|
#include <stdio.h>
|
|||
|
#include <tchar.h>
|
|||
|
|
|||
|
int strlen_my1(char s[])
|
|||
|
{
|
|||
|
char* p = s;
|
|||
|
while (*p++);
|
|||
|
return p - s - 1;
|
|||
|
}
|
|||
|
|
|||
|
int strlen_my2(char* s) {
|
|||
|
char* p = s;
|
|||
|
while (*p != '\0') {
|
|||
|
p++;
|
|||
|
}
|
|||
|
return p - s;
|
|||
|
}
|
|||
|
|
|||
|
int strcmp_my(const char str1[], const char str2[]) {
|
|||
|
int i = 0;
|
|||
|
while (str1[i] != '\0' && str2[i] != '\0' && str1[i] == str2[i])
|
|||
|
i++;
|
|||
|
return str1[i] - str2[i];
|
|||
|
}
|
|||
|
|
|||
|
void strcpy_my(char* dest, char* src)
|
|||
|
{
|
|||
|
while (*dest++ = *src++);
|
|||
|
}
|
|||
|
|
|||
|
void strcat_my(char* dest, char* src) {
|
|||
|
while (*dest) dest++;
|
|||
|
|
|||
|
while (*src) {
|
|||
|
*dest = *src;
|
|||
|
dest++;
|
|||
|
src++;
|
|||
|
}
|
|||
|
|
|||
|
*dest = '\0';
|
|||
|
}
|
|||
|
|
|||
|
void strchr_my(const char* str, int c, char** result) {
|
|||
|
while (*str != '\0') {
|
|||
|
if (*str == c) {
|
|||
|
*result = (char*)str;
|
|||
|
return;
|
|||
|
}
|
|||
|
str++;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
void test_strchr_my() {
|
|||
|
const char* str1 = "Hello, World!";
|
|||
|
const char* str2 = "This is a test string.";
|
|||
|
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 1: <20><><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 'o' <20> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> "Hello, World!"
|
|||
|
char* result1 = NULL;
|
|||
|
strchr_my(str1, 'o', &result1);
|
|||
|
if (result1 != NULL) {
|
|||
|
printf("%ld\n", result1 - str1);
|
|||
|
}
|
|||
|
else {
|
|||
|
printf("NULL\n");
|
|||
|
}
|
|||
|
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 2: <20><><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 'z' <20> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> "This is a test string."
|
|||
|
char* result2 = NULL;
|
|||
|
strchr_my(str2, 'z', &result2);
|
|||
|
if (result2 != NULL) {
|
|||
|
printf("%ld\n", result2 - str2);
|
|||
|
}
|
|||
|
else {
|
|||
|
printf("NULL\n");
|
|||
|
}
|
|||
|
}
|