commit1
This commit is contained in:
parent
ec452d24c3
commit
0a920e8a6e
77
main1
Normal file
77
main1
Normal file
@ -0,0 +1,77 @@
|
||||
#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.";
|
||||
|
||||
// Ïðèìåð 1: Ïîèñê ñèìâîëà 'o' â ñòðîêå "Hello, World!"
|
||||
char* result1 = NULL;
|
||||
strchr_my(str1, 'o', &result1);
|
||||
if (result1 != NULL) {
|
||||
printf("%ld\n", result1 - str1);
|
||||
}
|
||||
else {
|
||||
printf("NULL\n");
|
||||
}
|
||||
|
||||
// Ïðèìåð 2: Ïîèñê ñèìâîëà 'z' â ñòðîêå "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");
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user