字符串操作——C语言实现
发布日期:2021-05-14 09:05:09 浏览次数:27 分类:精选文章

本文共 1476 字,大约阅读时间需要 4 分钟。

Imports:
#include
#include
#include
Include necessary headers for string manipulation
Variables:
char ch1[] = { 'c', '+', '+' };
char ch2[] = { 'c', '+', '+', '\0' };
char* ch3 = "myC++";
char* ch4 = "good idea";
Function prototypes:
int strlen_new(const char* src);
char* strcat_new(char* strD, const char* strS);
void* memcpy_new(void* dst, void* src, size_t n);
void* memmove_new(void* dst, void* src, size_t n);
Implementing custom string functions:
- strlen_new: Counts characters in a string
- strcat_new: Concatenates two strings
- memcpy_new: Copies memory block
- memmove_new: Handles memory block moving with proper bounds checking
Testing functions:
int main()
{
int str_len = 0;
char* mem_src = "the src test memcpy";
char mem_dest[29] = "another hello";
char* mv_src = "the src test memmove";
char mv_dest[20];
// Testing memcpy
printf("Test memcpy result: %s\n", memcpy(mem_dest, mem_src, 20));
// Testing memmove
printf("Test memmove result: %s\n", memmove_new(mv_dest, mv_src, 10));
// String length test
str_len = strlen_new(ch3);
printf("Length of ch3: %d, string value: %s\n", str_len, ch3);
return 0;
}

Running results: memcpy test output: the src test memcpy memmove test output: the src tegood idea String length: 5, string value: good idea Comparison result: 6 (or -6, depending on comparison direction)

Program execution complete and returned 0.

上一篇:C语言之——__attribute__
下一篇:notepad++中设置python运行

发表评论

最新留言

路过,博主的博客真漂亮。。
[***.116.15.85]2025年04月17日 06时23分31秒