# include // // Program 10.2, Stephen Kochan, Programming in C. // int main ( void ); int stringLength ( const char string[] ); int main ( void ) { const char word1[] = { 'a', 's', 't', 'e', 'r', ' ', '\0' }; const char word2[] = { 'a', 't', '\0' }; const char word3[] = { 'a', 'w', 'e', '\0' }; // // WORD4 has an internal null character. // const char word4[] = { 'A', 'B', 'C', '\0', 'E', 'F', 'G', '\0' }; // // WORD5 is not properly terminated! // const char word5[] = { 'O', 'o', 'p', 's', 'y' }; printf ( "Length of \"%s\" is %i.\n", word1, stringLength ( word1 ) ); printf ( "Length of \"%s\" is %i.\n", word2, stringLength ( word2 ) ); printf ( "Length of \"%s\" is %i.\n", word3, stringLength ( word3 ) ); printf ( "Length of \"%s\" is %i.\n", word4, stringLength ( word4 ) ); // // However, at least with GCC, WORD5 prints out correctly, and // has length 5. // printf ( "Length of \"%s\" is %i.\n", word5, stringLength ( word5 ) ); return 0; } int stringLength ( const char string[] ) { int count = 0; while ( string[count] != '\0' ) { count = count + 1; } return count; }