#include<myhead.h>
#define Max_Size 100
//strlen函数
int my_strlen(const char *str)
{
int len = 0;
while(*str++ !='\0')
{
len++;
}
return len;
}
//strcpy函数
void my_strcpy(char *dest,const char *src)
{
while(*src) //遍历src将src赋值给dest
{
*dest++ = *src;
src++;
}
*dest = '\0';
}
//strcmp
int strcmp(const char *s1,const char *s2)
{
while(*s1) //遍历s1和s2比较
{
if(*s1 == *s2)
{
s1++;
s2++;
}
else
return *s1 - *s2;
if(*s1 == '\0')
return 0;
}
}
//strcat
char *my_strcat(char *dest,const char *src)
{
int i=0;
while(*dest) //遍历到dest末尾
{
dest++;
}
while(*src) //遍历src连接到dest的末尾
{
*dest++ =*src++;
}
*dest = '\0';
}
//子串
void my_strstr(const char *dest,const char *src)
{
while(*src) //遍历src
{
while(*src == *dest) //当dest首位等于src的某一位开始遍历dest
{
src++;
dest++;
if(*dest == '\0') //dest遍历结束标志,说明包含
{
printf("包含\n");
break;
}
else
{
continue;
}
}
src++;
if(*dest != '\0' && *src == '\0') //dest已经遍历结束,src还没遍历结束说明不包含
printf("不包含\n");
}
}
int main(int argc, const char *argv[])
{
char str[Max_Size];
char dest[Max_Size];
char src[Max_Size];
//strlen
//输入字符串
printf("strlen\n");
printf("请输入字符串:");
gets(str);
int res = my_strlen(str);
//输出
printf("字符串长度为:%d\n",res);
//strcpy
printf("strcpy\n");
printf("请输入字符串src:");
gets(src);
printf("请输入字符串dest:");
gets(dest);
printf("src:");
puts(src);
printf("dest:");
puts(dest);
my_strcpy(dest,src);
printf("复制后的dest:");
puts(dest);
//strcmp
printf("strcmp\n");
printf("请输入字符串1:");
gets(dest);
printf("请输入字符串2:");
gets(src);
int value = strcmp(dest,src);
printf("%d\n",value);
//strcat
printf("strcat\n");
printf("请输入字符串1:");
gets(dest);
printf("请输入字符串2:");
gets(src);
my_strcat(dest,src);
puts(dest);
//是否包含子串
printf("strstr\n");
printf("请输入字符串1:");
gets(dest);
printf("请输入字符串2:");
gets(src);
printf("字符串2是否包含字符串1:");
my_strstr(dest,src);
return 0;
}
标签:src,dest,基础,char,day10,printf,字符串,gets
From: https://blog.csdn.net/qq_55245263/article/details/140386584