char * strcpy ( char * destination, const char * source );
char * strcat ( char * destination, const char * source );
int strcmp ( const char * str1, const char * str2 );
模拟实现:strcpy strcat strcmp
#include<stdio.h>
#include<Windows.h>
char *Mystrcpy(char *dest, const char *src)
{
char *p = dest;
while (*dest++ = *src++)
return p;
}
char *Mystrcat(char *dest, const char*src)
{
int *pc = dest;
while (*dest != '\0')
{
dest++;
}
while (*dest++ = *src++)
{
;
}
return pc;
}
int Mystrcmp(const char *str1, const char *str2)
{
while (*str1 == *str2)
{
str1++;
str2++;
if (*str1 == '\0')
{
return 0;
}
}
if (*str1 > *str2)
{
return 1;
}
if (*str1 < *str2)
{
return -1;
}
}
int main()
{
char str1[20] = "abcd";
char *str2 = "efgh";
printf("str1=%s\n", Mystrcpy(str1, str2));
printf("str1=%s\n", Mystrcat(str1, str2));
printf("%d\n", Mystrcmp(str1, str2));
system("pause");
return 0;
}
str1=ebcd
str1=ebcdefgh
-1
请按任意键继续. . .
char * strncpy ( char * destination, const char * source, size_t num );
char * strncat ( char * destination, const char * source, size_t num );
int strncmp ( const char * str1, const char * str2, size_t num );
模拟实现:strncpy strncat strncmp
#include<stdio.h>
#include<Windows.h>
char *Mystrncpy(char *dest, const char* src, int count)
{
int i = 0;
char *p = dest;
while (i < count)
{
*dest++=*src++;
i++;
}
return p;
}
char *Mystrncat(char *dest, const char *src, int count)
{
int i = 0;
char *pc = dest;
while (*dest != '\0')
{
*dest++;
}
for (i = 0; i < count; i++)
{
*dest++ = *src++;
}
return pc;
}
int Mystrncmp(const char*str1, const char*str2, int count)
{
int i = 0;
int differ = 0;
while (i < count && ((differ = *str1 - *str2) == 0 )&& *str1 != '\0')
{
i++;
str1++;
str2++;
}
return differ;
}
int main()
{
char str1[20] = "abcd";
char *str2 = "efgh";
printf("str1=%s\n", Mystrncpy(str1, str2, 2));
printf("str1=%s\n", Mystrncat(str1, str2, 2));
printf("%d\n", Mystrncmp(str1, str2, 2));
system("pause");
return 0;
}
str1=efcd
str1=efcdef
0
请按任意键继续. . .