/* States.c (c) Kari Laitinen http://www.naturalprogramming.com/ 2013-11-24 File created. 2013-11-24 Last modification. This program demonstrates the use of the string functions in string.h. */ #include #include int main() { char states_in_usa[ 120 ] ; char westmost_state[] = "Hawaii" ; char prairie_state[] = "Illinois" ; strcpy( states_in_usa, westmost_state ) ; strcat( states_in_usa, " " ) ; strcat( states_in_usa, prairie_state ) ; printf( "\n %s", states_in_usa ) ; char golden_state[] = "California" ; /* We'll first move bytes in memory so that there will be enough space for the text " California". */ memmove( &states_in_usa[ 6 ] + strlen( golden_state ) + 2, &states_in_usa[ 6 ], strlen( &states_in_usa[ 6 ] ) ) ; /* Next, we'll copy two strings to the 'hole' in the string pointed by states_in_usa. */ strncpy( &states_in_usa[ 6 ] + 2, golden_state, strlen( golden_state ) ) ; strncpy( &states_in_usa[ 6 ], " ", 2 ) ; printf( "\n %s", states_in_usa ) ; char eastmost_state[] = "Maine" ; strcat( states_in_usa, " Virginia " ) ; strcat( states_in_usa, eastmost_state ) ; printf( "\n %s", states_in_usa ) ; char* pointer_to_last_state = strrchr( states_in_usa, ' ' ) ; /* By writing a zero, we can erase the last state in the list. */ *pointer_to_last_state = 0 ; strcat( states_in_usa, " Massachusetts" ) ; printf( "\n %s", states_in_usa ) ; strncpy( strstr( states_in_usa, "Illinois" ), "Michigan", 8 ) ; printf( "\n %s\n", states_in_usa ) ; } /* The output of this program is the following. Hawaii Illinois Hawaii California Illinois Hawaii California Illinois Virginia Maine Hawaii California Illinois Virginia Massachusetts Hawaii California Michigan Virginia Massachusetts */