自己实现str

strlen:

#include
int main(int argc, char*argv[])
{
	char a[128];
	gets(a);
	int i=0;
	while(a[i] != '\0')
	{
		i++;
	}
	printf("%d\n", i);
	return 0;
}

strcpy:

#include
int main(int argc, char* argv[])
{
	char* a1[10] = "hello";
	char* a2[10] = "world";
	int i = 0;
	int num = 0;
	while(a1[num]!='\0')
	{
		num++;
	}
	for(i=0;i<num;i++)
	{
		a2[i] = a1[i];
	}
	a2[i] = a1[i];
}

strcat:

#include
int main(int argc, char *argv[])
{
	char a1[20] = "hello";
	char a2[20] = "world";
	int i = 0;
	int j = 0;
	while(a1[i])
	{
		i++;
	}
	while(a2[j])
	{
		a1[i] = a2[j];
		i++;
		j++;
	}
	a1[i] = a2[i];
}

strcmp

#include
int main(int argc, char* argv)
{
	char a1[10] = "hello";
	char a2[10] = "world";
	int i = 0;
	while(a1[i] == a2[i] && a1[i] != '\0')
	{
		i++;
	}
	printf("%d\n", a1[i] - a2[i]);
	return 0;
}

你可能感兴趣的:(算法,linux)