// This program demonstrates a string in C and all the // wonderful things we can do with strings using our special // library // Week 4: Lecture 7: Strings (An array of char with a '\0') #include // You will have to include a string library here, if you want // to use all the wonderful functions that already exist for // strings! #include // Define the maximum size of the array #define MAX 6 int main(void) { // Declare an array of chars char array[MAX]; char array2[MAX]; // How would I put a word into my array from terminal? printf("Enter a word: "); fgets(array, MAX, stdin); printf("%s\n", array); /*printf("Enter a word: "); fgets(array2, MAX, stdin); printf("%s\n", array2); */ // What happens when I manipulate the string and change // things in the array (eg. move the null terminating char)? // Hello // Index 0 1 2 3 4 5 // Char h e l \0 /*array[3] = '\0'; printf("%s", array); printf("%c", array[3]); printf("%c", array[4]); */ // Example using strcpy to copy from one string // to another (destination, source) // Example using strlen to find string length // returns the int length NOT including '\0' printf("The length of the string is %d\n", strlen(array)); // Example using strcmp to compare two strings character // by character - function will return: // 0 = two strings are equal // other int if not the same printf("The two strings are the same: %d\n", strcmp(array, "hello")); return 0; }