#include <stdio.h>
#include <string.h>
// 功能:计算字符串s的长度、返回s的长度,不包括结束符NULL。
void my_strlen(char *str)
{
int lenth = 0;
while (*str++)
{
lenth++;
}
printf("lenth:\t%d\n",lenth);
}
/*比较字符串s1和s2;
(1)当s1<s2时,返回为负数,注意不是-1;
(2)当s1==s2时,返回值= 0;
(3)当s1>s2时,返回正数,注意不是1。
实际上是比较相应位字符的ASCII码大小.
如char*s1="abc";char*s2="bd";执行strcmp(s1,s2),其结果是负的.从两个字符串的头开始比,因为字符串s1中的'a'小于s2中的'b'所以s1<s2;
再如:char*s1="abc";char*s2="ad";其结果也是负的,因为s1中的'b'<s2中的'd'再如:char *s1="abc";char*s2="ab";其结果就是正的.因为s1中的'c'大于s2中的'\0';因为字符串的最后一个字符是\0;
再如:char *s1="abc";char *s2="abcd";其结果是负的.因为s1中的'\0'小于s2中的'd';
*/
int my_strcmp(char *str1,char *str2)
{
int ret = 0;
while(1)
{
ret = *str1 - *str2;
if(*str1 == '\0' || *str2 == '\0' || ret != 0)
{
break;
}
str1++;
str2++;
}
return ret;
}
//字符串复制
void my_strcpy(char *str,char *str_cpy)
{
while(*str)
{
*str_cpy++ = *str++;
}
*str_cpy ='\0';
}
//将指定长度的字符串复制。
void my_strncpy(char *str,char *str_ncpy,int num)
{
int i = 0;
int lenth = 0;
lenth = strlen(str);
if(num < lenth)
{
for(i = 0;i < num;i++)
{
*str_ncpy++ = *str++;
}
}
else
{
if(num == lenth)
{
for(i = 0;i < num;i++)
{
*str_ncpy++ = *str++;
}
*str_ncpy++ = '\0';
}
else
{
while (*str)
{
*str_ncpy++ = *str++;
}
}
}
*str_ncpy ='\0';
}
//字符串的拼接,将字符串str2拼接到str1的后面
void my_strcat(char *str1,char *str2)
{
char *temp = str1;
while(*temp != '\0')
{
temp++;
}
while(*str2)
{
*temp = *str2;
temp++;
str2++;
}
*temp = '\0';
printf("my_strcat:\t%s\n",str1);
}
//在字符串中查找字符,,打印该字符的地址,没有则打印NULL!
void my_strchr(char *str,char ch)
{
while(*str != 0)
{
if(ch == *str)
{
printf("the adress of the letter:\t%p\n",str);
break;
}
str++;
}
if(*str == 0)
{
printf("NULL!\n");
}
}
//在字符串中查找字符串,返回该字符串在字符串中出现的首地址,没有查找到返回NULL!
char my_strstr(char *str1,char *str2)
{
char *p = str1;
char *q = str2;
// if(*str2 == 0)
// {
// return printf("NULL!\n");
// }
if(*str1 == 0 || *str2 == 0)
{
return printf("NULL!\n");
}
while (*str1 && *str2)
{
if(*str1 != *str2)
{
str2 = q;
str1 = ++p;
}
str1++;
str2++;
}
if(*str2 == 0)
{
printf("the first adress of the string:\t%p\n",p);
}
}
int main()
{
int temp = 0;
int num = 0;
char ch1 = 0;
char ch2 = 0;
char str1[100] = {'\0'};
char str2[100] = {'\0'};
char str_cpy[100] = {'\0'};
char str_ncpy[100] = {'\0'};
printf("please input first string:\n");
gets(str1);
printf("first string:\n%s\n",str1);
my_strlen(str1);
my_strcpy(str1,str_cpy);
printf("str_cpy:\t%s\n",str_cpy);
printf("please input num:\n");
scanf("%d",&num);
getchar();
my_strncpy(str1,str_ncpy,num);
printf("str_ncpy:\t%s\n",str_ncpy);
printf("please input second string:\n");
gets(str2);
printf("second string:\n%s\n",str2);
my_strlen(str2);
my_strcpy(str2,str_cpy);
printf("str_cpy:\t%s\n",str_cpy);
printf("please input num:\n");
scanf("%d",&num);
getchar();
my_strncpy(str2,str_ncpy,num);
printf("str_ncpy:\t%s\n",str_ncpy);
printf("\n");
temp = my_strcmp(str1,str2);
printf("my_strcmp:\t%d\n",temp);
my_strcat(str1,str2);
printf("please input the letter what you need :\t%c\n",ch1);
scanf("%c",&ch1);
my_strchr(str1,ch1);
getchar();
printf("please input the letter what you need :\t%c\n",ch2);
scanf("%c",&ch2);
my_strchr(str1,ch2);
my_strstr(str1,str2);
return 0;
}