1. #include <stdio.h>
#include <assert.h>
size_t strlen (const char* s)
{
Return (assert (s), (*s? (Strlen (s+1) + 1): 0));
}
int main ()
{
printf ("%d\n", strlen (NULL));
return 0;
}
2. #include <stdio.h>
#include <assert.h>
char* strcpy (char* DST, const char* src)
{
char* ret = DST;
assert (DST && src);//Assert must invoke Assert, which is the idea of safe programming, the idea of error handling
while ((*dst++ = *src++)! = ' + ');
return ret;
}
Int main ()
{
char dst[20];
printf ("%s\n", strcpy (DST, "Delphi tang!"));
getch ();
return 0;
}
3. #include <stdio.h>
#include <malloc.h>
Int main ()
{
char s1[] = { ' H ', ' e ', ' l ', ' l ', ' o '};
char s2[] = {' H ', ' e ', ' l ', ' l ', ' o ', ' n '};
char* s3 = "Hello"; //read-only store
char* S4 = (char*) malloc (6*sizeof (char)); Heap
s4[0] = ' H ';
s4[1] = ' e ';
s4[2] = ' l ';
s4[3] = ' l ';
s4[4] = ' o ';
s4[5] = ' + ';
Free (S4);
return 0;
}
4. #include <stdio.h>
#include <assert.h>
size_t strlen (const char* s)
{
size_t length = 0;
ASSERT (s);
while (*s++)
{
length++;
}
return length;
}
int main ()
{
printf ("%d\n", strlen ("123456789"));
return 0;
}
String instances in C