#include size_t strlen(const char* s){ size_t len = 0; while(*s){ ++s; ++len; } return len; } /* Return 0 if the strings are the same * Return -1 if a comes before b * Return 1 if b comes before a */ int strcmp(const char* a, const char* b){ while(*a++ == *b++ && *a && *b); if(*a == 0 && *b == 0) return 0; if(*a > *b) return 1; return 0; } /* Parameters: Destination, source */ void strcpy(char *dest, const char* src){ printf("This is our strcpy\n"); while(*dest++ = *src++); } char* strchr(char* haystack, char needle){ for(;*haystack && *haystack != needle; ++haystack); if(*haystack) return haystack; return 0; } int main(){ const char *animal = "giraffe"; printf("%s\n", animal); char canimal[] = "giraffe"; printf("%s\n", canimal); if(strcmp(animal, canimal) == 0) printf("both are equal\n"); else printf("%s != %s\n", animal, canimal); if(strcmp("fox", "dog") > 0) printf("dog comes first\n"); else printf("fox comes first\n"); printf("The length of animal is %d\n", strlen(animal)); char copy[strlen(canimal)]; strcpy(copy, canimal); copy[1] = 'Y'; printf("canimal = %s, copy = %s\n", canimal, copy); if(strchr(animal, 'e')) printf("animal contains an e\n"); if(strchr(animal, 'z')) printf("animal contains a z\n"); *(strchr(canimal, 'r')) = 'R'; printf("canimal = %s\n", canimal); return 0; }