int strcmp(char *source, char *dest)函数

本文详细介绍了如何使用C语言实现字符串比较函数,并通过实例展示了其应用。
#include <stdio.h>

int strcmp_test(char*source, char *dest) 
{    
while ( (*source != '\0') &&(*source == *dest))   
 {
	source++; 
	dest++;   
}   
 return ( (*source) - (*dest) ) ? -1 : 0; 
}

void main()
{
      char str1[]="abcd";
	  char str2[]="abcD";
	  int i=strcmp_test(str1,str2);
	  printf("%d \n",i);
}

帮我添加一个函数,用于对比与memcpy的时间效率: /* * @Description: Unit test for bulk memory copy. * @Author: liruibo.tp-link.com.hk * @Date: 2025-07-16 17:05:04 * @LastEditors: liruibo.tp-link.com.hk * @LastEditTime: 2025-07-31 15:57:35 */ #include "bulk_mem_copy.h" #include <assert.h> #include <stdlib.h> #include <string.h> void test_normal_copy() { const char src[] = "hello world"; char dest[50] = {0}; bulk_mem_copy((unsigned char*)dest, (unsigned char*)src, strlen(src) + 1); assert(0 == strcmp(dest, src)); printf("[PASS] normal copy test.\n"); } void test_zero_length_copy() { int dest = 42; int src = 0; bulk_mem_copy((unsigned char*)&dest, (unsigned char*)&src, 0); assert(42 == dest); printf("[PASS] zero length copy test.\n"); } void test_overlap_copy() { char data[15] = "AAAAABBBBBCCCCC"; bulk_mem_copy((unsigned char*)data + 5, (unsigned char*)data, 10); assert(0 == strcmp(data, "AAAAAAAAAABBBBB")); printf("[PASS] overlap copy test.\n"); } void test_large_block_copy() { const size_t size = 1024 * 1024 * 10; char* src = malloc(size); char* dest = malloc(size); if (NULL == src || NULL == dest) { printf("[ERROR] large block copy test malloc failed.\n"); exit(EXIT_FAILURE); } memset(src, 0xAA, size); bulk_mem_copy((unsigned char*)dest, (unsigned char*)src, size); assert(0 == memcmp(src, dest, size)); free(src); src = NULL; free(dest); dest = NULL; printf("[PASS] large block copy test.\n"); } int main() { printf("STARTING UNIT TESTS...\n"); test_normal_copy(); test_zero_length_copy(); test_overlap_copy(); test_large_block_copy(); printf("ALL TESTS PASSED!\n"); return 0; }
08-01
#include<iostream> #include<cstdio> class hstring { unsigned int uselen; unsigned int usesize; char* cstr; public: hstring() ; const char* c_str() const;//使用接口访问 void set_string(const char* input); void append(const hstring& other); int find_string(const char* find); int sub_string(const char* sub); void modify_string(char* position, char* source, char* replace); }; hstring::hstring() :uselen(0), usesize(128), cstr(new char[usesize]) {//默认构造函数进行初始化 cstr[0] = '\0'; }; const char* hstring::c_str() const {//接口调用函数 return cstr; } void hstring::set_string(const char* input) { int i = 0; while (input[uselen] != '\0')++uselen;//读取字符串长度 //printf("%d\n", uselen); while (uselen > usesize - 1) { usesize *= 2; } char* new_cstr = new char[usesize]; while (input[i] != '\0' && i < usesize - 1) { new_cstr[i] = input[i]; ++i; } new_cstr[i] = '\0'; if (cstr != nullptr) { delete[]cstr; } cstr = new_cstr; } void hstring::append(const hstring& other) { int new_len = this->uselen + other.uselen; int new_size = usesize; if (new_len > usesize - 1) { usesize*=2; } char* new_str = new char[new_len]; int i = 0; for (i = 0; i<uselen; ++i) { new_str[i] = cstr[i]; } for (i = 0; i < other.uselen; ++i) { new_str[uselen + i] = other.cstr[i]; } new_str[new_len] = '\0'; delete[]cstr; cstr = new_str; uselen = new_len; usesize = new_size; } int hstring::find_string(const char* find) { if (cstr == nullptr) { printf("源字符串为空指针\n"); return -1; } if (find == nullptr) { printf("输入字符串为空指针\n"); return -1; } int main_len = uselen; int find_len = 0; while (find[find_len] != '\0')++find_len; if (find_len == 0) { printf("为空字符串,默认匹配位置为0\n"); return 0; } if (main_len < find_len) { printf("主字符串小于子字符串大小\n"); return -1; } for (int i = 0; i < main_len - find_len; ++i) { int j = 0; while(j < find_len && cstr[i + j] == find[j]){ ++j; } if (j == find_len) printf("找到字符串,位置:%d",i); return i; } printf("未找到字符串"); return -1; } int hstring::sub_string(const char* sub) { } //第一种简单直观的方法: int main() { hstring s; s.set_string("123456789"); printf("%s\n", s.c_str());//使用接口访问 hstring s1; s1.set_string("abc"); s.append(s1); printf("%s\n", s.c_str()); hstring s3; s3.set_string("456"); s.find_string(s3.c_str()); printf("%s\n", s3.c_str()); //std::cout << s << std::endl; //std::cout << uselen << std::endl; } 这是我已经写好的,现在就是就用C风格,并且不用hstring构造函数和拷贝构造函数、重载运算符的用法,不用string库就自己手搓实现再补充一点,不能用禁止使用的库函数:strlen、strcpy、find、strcat,做一个手搓的实现,让我了解底层
最新发布
08-14
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值