1.my_strlen()
函数原型:char *my_strlen( char *s)
功能:返回字符串的长度(不包含'\0')
函数定义及调用:
#include <stdio.h>
//函数定义(声明略);
int my_strlen(char *s)
{
int len = 0;
while(*(s++) != '\0') len++;
return len;
}
//函数的调用;
int main()
{
char s[] = {"sfjlefj"};
int len = my_strlen(s);
printf("length of s = %d",len);
return 0;
}
运行结果:
调试结果:
2,my_strcmp
原型:char *my_strcmp 90(const char *s1, const char *s2)
功能:返回两个字符串的差值
函数的定义和调用:
#include <stdio.h>
//函数定义(声明略);
int my_strcmp(const char *s1, const char *s2)
{
int len1 = 0, len2 = 0;
while(*(s1++) != '\0') len1++;
while(*(s2++) != '\0') len2++;
return len1-len2;
}
//函数的调用;
int main()
{
char a[] = {"123456"}, b[] = {"123"}, c[] = {"234567"};
printf("a,b的长度差:%d\n",my_strcmp(a,b));
printf("a,c的长度差:%d\n",my_strcmp(a,c));
return 0;
}
运行结果:
调试结果:
3,my_strcpy
原型:char *my_strcpy(char *s1, const char *s2)
功能:将 s2拷贝给s1,返回拷贝后字符串的首地址
函数的定义和调用:
#include <stdio.h>
#include <assert.h>
//函数的定义(声明略);
char *my_strcpy(char *s1, const char *s2)
{
assert((s1 != NULL) && (s2 != NULL));
while((*(s1++) = *(s2++)) != '\0');
char *s3 = s1;
return s3;
}
int main()
{
char s1[] = {"I hate study !"};
char s2[] = {"I love study !"};
my_strcpy(s1,s2);
printf("s1: %s\n",s1);
return 0;
}
运行结果:
调试结果:
4.my_strcat
原型:char *my_strcat(char *s1, const char *s2)
功能:将s2拼接在s1后,返回拼接后字符串的首地址
#include <stdio.h>
#include <assert.h>
//函数的定义(声明略)
char *my_strcat(char *s1, const char *s2)
{
assert((s1 != NULL) && (s2 != NULL));
char *s3 = s1;
while(*s1 != '\0') s1++;
while((*s1++ = *s2++) != '\0') ;
return s3;
}
//函数的调用
int main()
{
char s1[] = {"live is a fucking movie,"};
char s2[] = {"but I still love it."};
printf("%s",my_strcat(s1,s2));
return 0;
}
运行结果:
调试结果:
5.my_strchr
原型:char *my_strchr(char *s, char c)
功能:在字符串s中寻找字符c,并返回首个字符c的地址
函数的定义及调用:
#include <stdio.h>
#include <assert.h>
//函数的定义(声明略);
char *my_strchr(char *s, char c)
{
assert(s != NULL);
while((*s != c) && *s != '\0') s++;
if(*s == c) return s;
else return NULL;
}
//函数的调用
int main()
{
char s[] = {"I love study!"},c = 's';
char *p;
p = my_strchr(s,c);
*p = 't';
printf("s: %s",s);
return 0;
}
运行结果:
调试结果:
6.my_strstr()
函数原型:char *my_strstr(char *s1, char *s2)
功能:检索字串在字符串中首次出现的位置并返回
函数定义及调用:
#include <stdio.h>
#include <assert.h>
//函数的定义(声明略)
char *my_strstr(char *s1, char *s2)
{
assert((s1 != NULL) && (s2 != NULL));
char *_s1 = s1;
for(;*_s1 != '\0'; _s1++)
{
char *_s2 = s2;
if(*_s1 == *_s2)
{
char *__s1 = _s1;
while((*++__s1 == *++_s2) != '\0') ;
if(*_s2 == '\0')
return _s1;
}
}
return NULL;
}
//函数的调用
int main()
{
char s1[] = {"I hate study!"};
char s2[] = {"hate"};
char *p = my_strstr(s1,s2),*pp = p;
*p++ = 'l';
*p++ = 'o';
*p++ = 'v';
*p = 'e';
printf("s1:%s",s1);
return 0;
}
运行结果:
调试结果:
7.my_strncmp
原型:int my_strncmp(char s1[], char s2[], int )
函数的定义及调用:
#include <stdio.h>
//函数的定义(声明略)
int my_strncmp(char s1[], char s2[], int n)
{
int result = 0;
for(int i = 0; i < n; i++)
{
result += s1[i]-s2[i];
}
return result;
}
//函数的调用;
int main()
{
char s1[] = {"ABCD"};
char s2[] = {"ABA"};
printf("s1-s2 = %d",my_strncmp(s1,s2,3));
return 0;
}
运行结果:
调试结果:
8.my_strncpy
原型:char *my_strncpy(char s1[], char s2[], int n)
功能:从s2拷贝n个字符到s1,并返回拷贝后字符串首地址
函数的定义及调用:
#include <stdio.h>
//函数的定义(声明略);
char *my_strncpy(char s1[], char s2[],int n)
{
char *p = &s1[0];
char *pp = p;
for(int i = 0; s1[i] != '\0'; i++,pp++)
{
*pp = s1[i];
}
int i = 0;
while(i < n)
{
*pp++ = s2[i++];
}
return p;
}
//函数的调用;
int main()
{
char s1[] = {"ABCD"};
char s2[] = {"EFGH"};
char *s3 = my_strncpy(s1,s2,3);
printf("s3: %s",s3);
return 0;
}
运行结果:
调试结果:
9.strtok()的介绍和使用;
函数原型:char * strtok(char *s, const char *delim)
功能: 用来将字符串分割成一个个片段。参数s 指向欲分割的字符串,参数delim 则为分割字符串,当strtok()在参数s 的字符串中发现到参数delim 的分割字符时则会将该字符改为\0 字符。在第一次调用时,strtok()必需给予参数s 字符串,往后的调用则将参数s 设置成NULL。每次调用成功则返回下一个分割后的字符串指针。
使用:
#include <stdio.h>
#include <string.h>
main()
{
char s[] = "ab-cd : ef;gh :i-jkl;mnop;qrs-tu: vwx-y;z";
char *delim = "-: ";
char *p;
printf("%s ", strtok(s, delim));
while((p = strtok(NULL, delim)))
printf("%s ", p);
printf("\n");
}
运行结果:
总结
经过这次作业,我学到了很多。不仅是对指针,对字符串的理解,还积累了很多写博客的心得。
继续加油^_^