C/C++字符串函数my_strcpy和字符串操作

#include "stdlib.h"
#include "stdio.h"
#include "string.h"

void my_strcpy(char* target, char* source) {

    //for (; *source != '\0'; target++, source++) {
    //    *target = *source;
    //}
    //*target = '\0';

    //while (*source != '\0') {
    //    *target = *source;
    //    target++;
    //    source++;
    //}
    //*target = '\0';

    //while (*source != '\0') {
    //    *target++ = *source++;  // 后置++,现实执行,后++
    //}
    //*target = '\0';

    //while ((*target++ = *source++) != '\0') {
    //
    //}

    //while ((*target++ = *source++) != '\0');

    while (*target++ = *source++);
}
void main() {

    char* ptr1 = "aoe";
    char* ptr2 = (char*)malloc(128);
    char ptr3[128];
    my_strcpy(ptr2, ptr1);
    my_strcpy(ptr3, ptr1);

    printf("ptr2:%s\n", ptr2);
    printf("ptr3:%s\n", ptr3);


    // linux strcpy拷贝源代码
    //char* strcpy(char* dst, const char* src)
    //{
    //    char* cp = dst;
    //    while (*cp++ = *src++);  /* Copy src over dst */
    //    return(dst);
    //}

    system("pause");
}

-------------------------------
void main02() {

    char *str1 = "Hallo, World! Hi C++", *str2 = "Hi", *ptr;
    ptr = strstr(str1, str2);
    printf("the substring is:%s\n", ptr);
 
    system("pause");
}
-------------------------------
// do...while
void main() {

    // 找出aoe出现的次数
    char* ptr = "aoekdikd21aoe23dfiaoekckoiaodkakdaoe28hahaaoe";
    int count = 0;
    do {
        ptr = strstr(ptr, "aoe");
        if (ptr == NULL) { // 如果没有找到aoe跳出循环
            break;
        } else {
            count++;
            ptr = ptr + strlen("aoe");
        }

    } while (*ptr != '\0');

    printf("count:%d\n", count);

    system("pause");
}
-------------------------------
//while
void main() {

    // 找出aoe出现的次数
    char* ptr = "aoekdikd21aoe23dfiaoekckoiaodkakdaoe28hahaaoe";
    int count = 0;
    while (ptr = strstr(ptr, "aoe")) {
        ptr = ptr + strlen("aoe");
        count++;
        /*if (*ptr == '\0') {
            break;
        }*/
    }

    printf("count:%d\n", count);

    system("pause");
}
-------------------------------
函数的接口封装和设计
优化:放在函数中,函数化,加入异常检查
字符串操作工程开发模型:业务模型和业务测试模型分离

// http://aospxref.com/ android系统源代码在线代码
// https://cs.android.com android系统源代码在线代码
// http://androidxref.com/kernel_3.18/xref/lib/string.c android系统和kernel在线代码
// https://elixir.bootlin.com/linux/latest/source  linux kernel在线代码

/** string.c strstr代码
 * strstr - Find the first substring in a %NUL terminated string
 * @s1: The string to be searched
 * @s2: The string to search for
 */
char* strstr(const char* s1, const char* s2)
{
    size_t l1, l2;

    l2 = strlen(s2);
    if (!l2)
        return (char*)s1;
    l1 = strlen(s1);
    while (l1 >= l2) {
        l1--;
        if (!memcmp(s1, s2, l2))
            return (char*)s1;
        s1++;
    }
    return NULL;
}

#define OK 0
#define Error -1

int getCount(char* str1, char* str2, int* out_count) {

    char* ptr = str1;
    int count = 0;
    int ret = 0;

    if (str1 == NULL || str2 == NULL || out_count == NULL) {
        ret = Error;
        printf("Error: getCount() return error:%d\n", ret);
        return ret;
    }

    do {
        ptr = strstr(ptr, str2);
        if (ptr == NULL) { // 如果没有找到aoe跳出循环
            break;
        }
        else {
            count++;
            ptr = ptr + strlen(str2);
        }

    } while (*ptr != '\0');

    *out_count = count;

    return OK;
}

void main() {

    // 找出aoe出现的次数
    char* str1 = "aoekdikd21aoe23dfiaoekckoiaodkakdaoe28hahaaoe";
    char* str2 = "aoe";
    int count = 0;
    int ret = 0;

    ret = getCount(str1, str2, &count);
    if (ret != OK) {
        printf("Error: getCount() error:%d\n", ret);
        return;
    }
    printf("count:%d\n", count);

    system("pause");
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值