#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<assert.h>
size_t Strlen(const char* str) {
int i;
size_t size = 0;
assert(str!=NULL);
for (i=0;str[i]!= '\0';i++) {
size++;
}
return size;
/*const char* p = str;
for (;*str != '\0';str++) {
}
return str - p;
*/
}
int main() {
char* a= "abcdef";
printf("%d\n", Strlen(a));
system("pause");
return 0;
}
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<assert.h>
char* Strcat(char* dest, const char* src) {
assert(dest != NULL);
assert(src != NULL);
int i = 0;
for (;dest[i] != '\0';i++);
for (int j = 0;src[j] != '\0';i++,j++) {
dest[i] = src[j];
}
dest[i] = '\0';
return dest;
}
int main() {
char str1[1024] = "abc";
char str2[1024] = "def";
Strcat(str1, str2);
printf("%s\n", str1);
printf("%s\n", str2);
system("pause");
return 0;
}
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<assert.h>
const char* Strstr(const char* str1, const char* str2) {
assert(str1 != NULL);
assert(str2 != NULL);
if (*str2 == '\0') {
return NULL;
}
const char* black_ptr = str1;
while (*black_ptr != '\0') {
const char* red_ptr = black_ptr;
const char* sub_ptr = str2;
while (*red_ptr != '\0'
&&*sub_ptr != '\0'
&&*red_ptr == *sub_ptr) {
++sub_ptr;
++red_ptr;
}
if (*sub_ptr == '\0') {
return black_ptr;
}
++black_ptr;
}
return NULL;
}
int main() {
char str1[] = "abcdef";
char str2[] = "bcd";
const char* ret = Strstr(str1, str2);
printf("%p\n", str1);
printf("%p\n", ret);
system("pause");
return 0;
}
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<assert.h>
int Strcmp(char* str1, char*str2) {
assert(str1 != NULL);
assert(str2 != NULL);
int i = 0;
for (;str1[i] != '\0'&&str2[i] != '\0';i++) {
if (str1[i] < str2[i]) {
return -1;
}
else if (str1[i] > str2[i]) {
return 1;
}
}
if (str1[i] < str2[i]) {
return -1;
}
else if (str1[i] > str2[i]) {
return 1;
}
else {
return 0;
}
}
int main() {
char str1[1024] = "bbbabc";
char str2[1024] = "bbb";
int ret = Strcmp(str1, str2);
if (ret < 0) {
printf("str1<str2\n");
}
else if (ret > 0) {
printf("str1>str2\n");
}
else if(ret==0){
printf("str1==str2\n");
}
system("pause");
return 0;
}
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void* Memcpy(void*dest,const void*src,size_t num) {
size_t i = 0;
char* pdest = (char*)dest;
const char* psrc = (const char*)src;
for (i = 0;i < num;i++) {
pdest[i]= psrc[i];
}
return dest;
}
int main() {
int arr1[4] = { 0 };
int arr2[4] = { 1,2,3,4 };
Memcpy(arr1, arr2, 16);
for (int i = 0;i < 4;i++) {
printf("%d\n", arr2[i]);
}
system("pause");
return 0;
}
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void* Memmove(void*dest, const void*src, size_t num) {
size_t i = 0;
char* pdest = (char*)dest;
const char* psrc = (const char*)src;
if (pdest >= psrc && pdest < (psrc + num)) {
for (i = num;i >0;i--) {
pdest[i] = psrc[i];
}
}
else {
for (i = 0;i < num;i++) {
pdest[i] = psrc[i];
}
}
return dest;
}
int main() {
int arr1[4] = { 0 };
int arr2[4] = { 1,2,3,4 };
Memmove(arr1, arr2, 12);
for (int i = 0;i < 4;i++) {
printf("%d\n", arr1[i]);
}
system("pause");
return 0;
}
模拟实现C语言中字符串函数
最新推荐文章于 2022-07-21 09:00:26 发布