1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
|
int my_strlen(const char s[])
{ int n = 0; while (s[n]) ++n; return n; }
int my_strcmp(char const* a, char const* b)
{ while (*a && *a == *b) ++a, ++b; return *a - *b; }
void my_strcpy(char dst[], const char src[])
{ do *dst++ = *src; while (*src++); }
void my_strcat(char dst[], const char src[])
{ dst += my_strlen(dst); my_strcpy(dst, src); }
#include <cstdio>
#include <cstring>
int main()
{
{
char s1[] = "";
char s2[] = "!!!Hello World!!!";
char s3[] = "What do you want to set aside time for?";
char const* fmt = "my_strlen(\"%s\") = %2d\n";
std::printf(fmt, s1, my_strlen(s1));
std::printf(fmt, s2, my_strlen(s2));
std::printf(fmt, s3, my_strlen(s3));
}
std::puts("");
{
char s1[] = "";
char s2[] = "!!!Hello World!!!";
char s3[] = "What do you want to set aside time for?";
char buf[sizeof s3 + sizeof s2 + sizeof s1];
char const* fmt = "my_strcpy(buf, \"%s\"); buf = \"%s\"\n";
my_strcpy(buf, s1); std::printf(fmt, s1, buf);
my_strcpy(buf, s2); std::printf(fmt, s2, buf);
my_strcpy(buf, s3); std::printf(fmt, s3, buf);
my_strcpy(buf, s2); std::printf(fmt, s2, buf);
}
std::puts("");
{
char s1[] = "";
char s2[] = "!!!Hello World!!!";
char s3[] = "What do you want to set aside time for?";
char buf[sizeof s3 + sizeof s2 + sizeof s1 + sizeof s2];
char const* fmt = "my_strcat(buf, \"%s\"); buf = \"%s\"\n";
my_strcat(buf, s1); std::printf(fmt, s1, buf);
my_strcat(buf, s2); std::printf(fmt, s2, buf);
my_strcat(buf, s3); std::printf(fmt, s3, buf);
my_strcat(buf, s2); std::printf(fmt, s2, buf);
my_strcat(buf, s1); std::printf(fmt, s1, buf);
}
std::puts("");
{
char s1[] = "";
char s2[] = "!!!Hello World!!!";
char s3[] = "What do you want to set aside time for?";
char const* fmt = "my_strcmp(\"%s\", \"%s\") = %2d\n";
std::printf(fmt, s1, s1, my_strcmp(s1, s1));
std::printf(fmt, s1, s2, my_strcmp(s1, s2));
std::printf(fmt, s1, s3, my_strcmp(s1, s3));
std::printf(fmt, s2, s1, my_strcmp(s2, s1));
std::printf(fmt, s2, s3, my_strcmp(s2, s3));
std::printf(fmt, s3, s1, my_strcmp(s3, s1));
std::printf(fmt, s2, s2, my_strcmp(s2, s2));
}
}
|