C語言:模擬中常用字符串函數

strlen

int my_strlen(const char *pc)
{
	int i = 0;
	while (pc[i])
		i++;
	return i;
}

 

strcat

char * my_strcat(char *pc1, const char *pc2)
{
	
	int i = 0, j = 0;
	while (pc1[i]) 
		i++;
	while (pc2[j]) 
		pc1[i++] = pc2[j++];
	pc1[i] = '\0';
	return pc1;

}

strcpy

char * my_strcpy(char *pc1, const char *pc2)
{
	int i = 0;
	while (pc2[i])
	{
		pc1[i] = pc2[i];
		i++;
	}
	pc1[i] = '\0';
	return pc1;
}

strcmp

int my_strcmp(const char *pc1, const char *pc2)
{
	while (*pc1 == *pc2)
	{
		if (*pc1 == '\0')
			return 0;
		pc1++;
		pc2++;
	}
	return *pc1 - *pc2;
}

strstr在下一篇哦

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章