// This program calculates the length of a string // without using strlen function // Week 4 Lecture 8 #include // Define the size of the array = this is clearly going to cause some // waste - one of the downfalls of arrays #define MAX_LENGTH 124 int main(void) { // Declare our array to hold the string char word[MAX_LENGTH]; int length = 0; printf("Type in a word: "); // Read in the string using fgets() function fgets(word, MAX_LENGTH, stdin); // Do some magic here to calculate: // How do we keep looping through the string, how do we // know when the string has finished? int i = 0; while (word[i] != '\0') { length++; i++; } printf("There are %d characters in the word: %s\n", length, word); return 0; }