#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");
}
C/C++字符串函数my_strcpy和字符串操作
于 2022-08-13 20:46:25 首次发布