10.2作业

#include 
#include 
#include 
/*
 * function:    实现strcpy、strcmp、strcat的代码实现
 * @param [ in] 
 * @param [out] 
 * @return      
 */
char *my_strcpy(char *dest, const char *src){
	char* temp=dest;
	while(*src!='\0')
		*temp++ = *src++;
	*temp=*src;
	return dest;
}
int my_strcmp(const char *s1, const char *s2){
	while(*s1!='\0'&&*s2!='\0'){
		if(*s1++==*s2++)
			continue;
		s1--;
		s2--;
	//printf("s1=%c %d;s2=%c %d\n",*s1,*s1,*s2,*s2);
		return (int)(*s1-*s2);
	}
	//printf("break:s1=%c %d;s2=%c %d\n",*s1,*s1,*s2,*s2);
	return (int)(*s1-*s2);
}
char *my_strcat(char *dest, const char *src){
	char* temp=dest;
	while(*++temp!='\0');
	while(*src!='\0')
		*temp++=*src++;
	
	*temp=*src;
	return dest;
}
int main(int argc, const char *argv[])
{
	char s1[10]="hello";
	char s2[10]="";

	printf("my_strcpy:%s\n",my_strcpy(s2,"jinitaimei"));
	printf("s2=%s\n",s2);
	printf("my_strcmp:%d\n",my_strcmp(s1,"hell"));
	printf("my_strcmp:%d\n",my_strcmp(s1,"hello"));
	printf("my_strcmp:%d\n",my_strcmp(s1,"hella"));
	printf("my_strcmp:%d\n",my_strcmp(s1,"hellz"));
	printf("my_strcmp:%d\n",my_strcmp(s1,"helloo"));
	printf("my_strcat:%s\n",my_strcat(s1,"longtimenosee"));
	printf("s1=%s\n",s1);
	return 0;
}

运行结果:

10.2作业_第1张图片

你可能感兴趣的:(c语言)