/* * Very very simple program illustrating two ways to work with strings. */ #include /* * two functions to get length of string * purely illustrative/pedagogical since the standard library has strlen */ int string_length_array(char s[]); int string_length_pointer(char * s); int main(void) { printf("string '%s'\n", "hello world"); printf("length %d\n", string_length_array("hello world")); printf("length %d\n", string_length_pointer("hello world")); printf("string '%s'\n", "bye!"); printf("length %d\n", string_length_array("bye!")); printf("length %d\n", string_length_pointer("bye!")); return 0; } int string_length_array(char s[]) { int index = 0; while (s[index] != '\0') ++index; return index; } int string_length_pointer(char * s) { char * p = s; while (*p != '\0') ++p; return (p-s); }