/* Capitals.c (c) Kari Laitinen http://www.naturalprogramming.com/ 2013-11-22 File created. 2013-11-22 Last modification. */ #include #include int main() { char countries_and_capitals[] = "Finland Helsinki Usa Washington Denmark Copenhagen " "Afghanistan Kabul Russia Moscow England London " "Italy Rome France Paris Spain Madrid " "Portugal Lisbon Chile Santiago Japan Tokyo " "Sweden Stockholm Norway Oslo Pakistan Islamabad " "Iceland Reykjavik Hungary Budapest Holland Amsterdam " "Belgium Brussels Austria Vienna Israel Jerusalem " ; char country_name[ 20 ] ; char capital_name[ 20 ] ; int character_index ; char* name_from_list ; char* text_after_country_name ; printf( "\n This program may know the capital of a country." "\n Type in a country name: " ) ; gets( country_name ) ; for ( character_index = 0 ; character_index < strlen( country_name ) ; character_index ++ ) { country_name[ character_index ] = country_name[ character_index ] | 0x20 ; } country_name[ 0 ] = country_name[ 0 ] & 0xDF ; /* Now country name should be in lowercase letters with the first letter capitalized. */ name_from_list = strstr( countries_and_capitals, country_name ) ; if ( name_from_list != 0 ) { /* The given country name was found in the list. */ text_after_country_name = strstr( name_from_list, " " ) ; /* We suppose that there is exactly one space character after each country name. We'll skip the space, and copy the capital name. */ text_after_country_name ++ ; character_index = 0 ; while ( *text_after_country_name != ' ' && *text_after_country_name != 0 ) { capital_name[ character_index ] = *text_after_country_name ; character_index ++ ; text_after_country_name ++ ; } /* Let's terminate the capital_name string. */ capital_name[ character_index ] = 0 ; printf( "\n The capital of %s is %s \n", country_name, capital_name ) ; } }