// Lecture example for COMP1511/1911 Week 4 Thursday // Some simple examples demonstrating how strings work in C #include #define MAX_LEN 100 void print_string(char s[]); int main(void) { // What do these look like in memory char favourite_food[] = {'k', 'f', 'c', '\0'}; //What if we leave off '\0 char favourite_drink[] = "kombucha"; // Includes the '\0' at the end for us char favourite_book[MAX_LEN] = "Folk of the Faraway Tree"; //favourite_book = "Folk of the Faraway Tree"; // Can print out characters printf("%c\n",favourite_food[0]); printf("%c\n",favourite_drink[3]); // Can print out the whole string printf("%s\n", favourite_food); printf("%s\n", favourite_drink); printf("%s\n", favourite_book); favourite_drink[0] = 'Z'; printf("%s\n", favourite_drink); favourite_drink[4] = '\0'; printf("%s\n", favourite_drink); print_string(favourite_food); return 0; } // Our own version of printf("%s") void print_string(char s[]) { int i = 0; while (s[i] != '\0') { printf("%c", s[i]); i++; } }