字符串操作——C语言实现
C语言 实现 操作 字符串
2023-09-11 14:15:08 时间
代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
char ch1[]={ 'c', '+', '+'};
char ch2[]={ 'c', '+', '+', '\0'};
char ch3[] = "myC++";
char ch4[] = "good idea";
int strlen_new(const char* src);//const 2'
char* strcat_new(char *strD, const char *strS);
char* strcpy_new(char *strD, const char *strS);
int strcmp_new(const char *s1, const char *s2);
void* memcpy_new(void* dst, void* src, size_t n);
void* memmove_new(void* dst, void* src, size_t n);
void* memmove_new(void* dst, void* src, size_t n)
{
char * dp = (char*)dst;
char * sp = (char*)src;
assert(src != NULL && dst != NULL && n>0);
if(sp > dp || (sp + n) < dp)
{
while(n--)
{
*(dp++)=*(sp++);
}
*dp='\0';
}
else if (sp < dp)
{
sp += n;
dp += n;
*dp = '\0';
while(n--)
*(--dp) = *(--sp);
}
return dst;
}
void* memcpy_new(void* dst, void* src, size_t n)
{
char* dp = (char*)dst;
char* sp = (char*)src;
assert(src != NULL && dst != NULL && n>0);
while(n--)
{
*dp = *sp;
dp++;
sp++;
}
dp = '\0';
return dst;
}
int strlen_new(const char* src)
{
int count = 0;
assert(src != NULL);
while(*src++ != '\0')
{
count++;
}
return count;
}
char * strcat_new(char * strD, const char * strS)
{
char * add = strD;
assert(strD != NULL && strS != NULL);
while(*strD != '\0')
strD++;
while((*strS) != '\0')
{
*strD = *strS;
strD++;
strS++;
}
*strD = '\0';
//while(*strD++ = *strS++);//cat more efficient
return add;
}
char* strcpy_new(char *strD, const char *strS)
{
char * add = strD;
assert(strD != NULL && strS != NULL);
while(*strS != '\0')
*strD++ = *strS++;
//attention姝ゅ锛岃嫢*strS涓衡€橽0鈥橈紝鍒欏叾鍏堣祴鍊肩粰strD锛屾晠鏈€鍚庝笉闇€瑕佸啀娣诲姞'\0'
//while(*strD++ = *strS++);
*strD = '\0';
return add;
}
int strcmp_new(const char *s1, const char *s2)
{
//int ret;
assert(s1 != NULL && s2 != NULL);
while(*s1 && *s2 && *s1 == *s2)
{
s1++;
s2++;
}
return *s1 - *s2;
//return *s2 - *s1;
}
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];
printf("Test memcpy ret is :%s\n", memcpy(mem_dest, mem_src, 20));
printf("Test memmove ret is :%s\n", memmove_new(mv_dest, mv_src, 10));
str_len = strlen_new(ch3);
//printf("%s\n", strcat_new(ch3, ch4));
printf("%s\n", strcpy_new(ch3,ch4));
printf("strcmp = %d\n", strcmp_new(ch3,ch4));
printf("len = %d ,%s\n", str_len, ch3);
return 0;
}
运行结果如下:
Test memcpy ret is :the src test memcpy
Test memmove ret is :the src te
good idea
strcmp = 6
len = 5 ,good idea
Process returned 0 (0x0) execution time : 0.047 s
Press any key to continue.
相关文章
- C语言程序设计100例之(60):集合
- 数据结构之---C语言实现图的数组(邻接矩阵)存储表示
- C语言之const
- C语言通讯录的实现
- 数据结构考研 算法直接插入排序 C语言实现
- 二叉搜索树BST(C语言实现可用)
- Go与C语言的互操作 cgo
- 【C语言】递归实现n的k次方
- 基于C语言实现(控制台)校医院预约挂号管理系统【100010729】
- 基于C语言实现(控制台)校园外来人员进出监控与管理系统【100010720】
- 基于Python实现一个C语言的编译器【100010711】
- 基于C语言+MySQL实现(控制台)图书馆借书系统【100010553】
- 基于C语言实现高效率 IP 路由查找的内容的实验【100010466】
- 初始C语言(2)
- 《数据结构与算法 C语言版》—— 1.2数据结构的发展概况
- 《数据结构与算法 C语言版》—— 第2章 线性表
- PID原理以及C语言的代码实现
- 动态内存管理 ——C语言
- C语言实现惯导参数的初始化
- C语言实现等效旋转矢量与三维矢量相乘
- C语言实现等效旋转矢量转方向余弦矩阵和姿态四元数
- C语言实现printf同行输出(刷新显示)
- 百度美团笔试题——迷宫问题,进来测测数据结构和C语言是否过关
- 计数排序——C语言实现
- C语言指针练习
- 【数据结构】----C语言实现栈操作
- 第三章 C语言输入与输出