C语言作业2--- 自定义一些字符串函数并调试检验

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");
}

运行结果:


                                                          总结 

经过这次作业,我学到了很多。不仅是对指针,对字符串的理解,还积累了很多写博客的心得。

继续加油^_^ 

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Front row sailor

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值