Here is my Code.
spotifyApi.getMyCurrentPlaybackState()
.then((response) => {
this.setState({
nowPlaying: {
name: response.item.name,
artist: response.item.artists.name,
albumArt: response.item.album.images[0].url
}
});
})
my response object for 'artist:' is not displaying the artists' name. How can I change
artist: response.item.artists.name,
to get "Key Glock" to display?
Here is the network response data.
"item" : {
"album" : {
"album_type" : "album",
"artists" : [ {
"external_urls" : {
"spotify" : "https://open.spotify.com/artist/0RESbWvOMyua0yuyVrztJ5"
},
"href" : "https://api.spotify.com/v1/artists/0RESbWvOMyua0yuyVrztJ5",
"id" : "0RESbWvOMyua0yuyVrztJ5",
"name" : "Key Glock",
"type" : "artist",
"uri" : "spotify:artist:0RESbWvOMyua0yuyVrztJ5"
} ],
"available_markets" : [ "AD", "AE", "AG", "AL", "AM", "AO", "AR", "AT", "AU", "AZ", "BA", "BB", "BD", "BE", "BF", "BG", "BH", "BI", "BJ", "BN", "BO", "BR", "BS", "BT", "BW", "BY", "BZ", "CA", "CD", "CG", "CH", "CI", "CL", "CM", "CO", "CR", "CV", "CW", "CY", "CZ", "DE", "DJ", "DK", "DM", "DO", "DZ", "EC", "EE", "EG", "ES", "FI", "FJ", "FM", "FR", "GA", "GB", "GD", "GE", "GH", "GM", "GN", "GQ", "GR", "GT", "GW", "GY", "HK", "HN", "HR", "HT", "HU", "ID", "IE", "IL", "IN", "IQ", "IS", "IT", "JM", "JO", "JP", "KE", "KG", "KH", "KI", "KM", "KN", "KR", "KW", "KZ", "LA", "LB", "LC", "LI", "LK", "LR", "LS", "LT", "LU", "LV", "LY", "MA", "MC", "MD", "ME", "MG", "MH", "MK", "ML", "MN", "MO", "MR", "MT", "MU", "MV", "MW", "MX", "MY", "MZ", "NA", "NE", "NG", "NI", "NL", "NO", "NP", "NR", "NZ", "OM", "PA", "PE", "PG", "PH", "PK", "PL", "PS", "PT", "PW", "PY", "QA", "RO", "RS", "RU", "RW", "SA", "SB", "SC", "SE", "SG", "SI", "SK", "SL", "SM", "SN", "SR", "ST", "SV", "SZ", "TD", "TG", "TH", "TJ", "TL", "TN", "TO", "TR", "TT", "TV", "TW", "TZ", "UA", "UG", "US", "UY", "UZ", "VC", "VE", "VN", "VU", "WS", "XK", "ZA", "ZM", "ZW" ],
"external_urls" : {
"spotify" : "https://open.spotify.com/album/7snqOEQEtKqxJw3NTGml6i"
artists is an array, presumably the spotify API would return multiple artists if you had two or more artists on a track, which we would normally write "Jim feat. John".
To get the first artist's name: response.item.album.artists[0].name
To get all the artists' names: response.item.album.artists.map(curr => curr.name)
I am writing an autocomplete service and want to get only country and city calling a Microsoft Azure service - with no addresses and POI or such.
I currently found Address Search where I would need to call https://atlas.microsoft.com/search/address, but it returns addresses, not cities and countries - which leads to many duplicates.
Plus, If I need to have a fixed number of auto complete options - say, 15 - it is going to require multiple calls to the service until I have all 15. On top of that, I will never know if there are more results for a typed partial country/city combination or not.
I don't want to use a fixed list as it needs maintenance.
I would not want to use another data provider for now, if it is possible to achieve with Azure.
This isn't supported yet, but is planned for the future. The closest that could be done now would be to use the https://learn.microsoft.com/en-us/rest/api/maps/search/getsearchaddressstructured service but it requires a country code to be specified, so you would only get cities for the specified country. For example, passing in new as the municipality as done in the following query:
https://atlas.microsoft.com/search/address/structured/json?api-version=1&municipality=new&countryCode=US&subscription-key=<Your_Key>
will return the following city results:
New York
New Orleans
New Braunfels
New Port Richey
New Bern
New Haven
New Iberia
New Castle
New Bedford
New Albany
Here is the raw response
{
"summary": {
"query": "new",
"queryType": "NON_NEAR",
"queryTime": 23,
"numResults": 10,
"offset": 0,
"totalResults": 998,
"fuzzyLevel": 1
},
"results": [
{
"type": "Geography",
"id": "US/GEO/p0/1167",
"score": 2.6649377346,
"entityType": "Municipality",
"address": {
"municipality": "New York",
"countrySubdivision": "NY",
"countrySubdivisionName": "New York",
"countryCode": "US",
"country": "United States",
"countryCodeISO3": "USA",
"freeformAddress": "New York, NY"
},
"position": {
"lat": 40.71305,
"lon": -74.00723
},
"viewport": {
"topLeftPoint": {
"lat": 40.9175,
"lon": -74.25564
},
"btmRightPoint": {
"lat": 40.49587,
"lon": -73.70027
}
},
"boundingBox": {
"topLeftPoint": {
"lat": 40.9175,
"lon": -74.25564
},
"btmRightPoint": {
"lat": 40.49587,
"lon": -73.70027
}
},
"dataSources": {
"geometry": {
"id": "d49d86a4-0f4b-45f2-b607-31a84d02af00"
}
}
},
{
"type": "Geography",
"id": "US/GEO/p0/90711",
"score": 2.5113995075,
"entityType": "Municipality",
"address": {
"municipality": "New Orleans",
"countrySecondarySubdivision": "Orleans",
"countrySubdivision": "LA",
"countrySubdivisionName": "Louisiana",
"countryCode": "US",
"country": "United States",
"countryCodeISO3": "USA",
"freeformAddress": "New Orleans, LA"
},
"position": {
"lat": 29.95176,
"lon": -90.07462
},
"viewport": {
"topLeftPoint": {
"lat": 30.21747,
"lon": -90.24051
},
"btmRightPoint": {
"lat": 29.86582,
"lon": -89.62506
}
},
"boundingBox": {
"topLeftPoint": {
"lat": 30.21747,
"lon": -90.24051
},
"btmRightPoint": {
"lat": 29.86582,
"lon": -89.62506
}
},
"dataSources": {
"geometry": {
"id": "0000554c-4100-3c00-0000-0000596a7628"
}
}
},
{
"type": "Geography",
"id": "US/GEO/p0/103135",
"score": 2.4384012222,
"entityType": "Municipality",
"address": {
"municipality": "New Braunfels",
"countrySubdivision": "TX",
"countrySubdivisionName": "Texas",
"countryCode": "US",
"country": "United States",
"countryCodeISO3": "USA",
"freeformAddress": "New Braunfels, TX"
},
"position": {
"lat": 29.70988,
"lon": -98.11973
},
"viewport": {
"topLeftPoint": {
"lat": 29.91293,
"lon": -98.34627
},
"btmRightPoint": {
"lat": 29.59298,
"lon": -97.9908
}
},
"boundingBox": {
"topLeftPoint": {
"lat": 29.91293,
"lon": -98.34627
},
"btmRightPoint": {
"lat": 29.59298,
"lon": -97.9908
}
},
"dataSources": {
"geometry": {
"id": "9d7bf985-115b-423a-979a-97cc093c3166"
}
}
},
{
"type": "Geography",
"id": "US/GEO/p0/23041",
"score": 2.4345231056,
"entityType": "Municipality",
"address": {
"municipality": "New Port Richey",
"countrySecondarySubdivision": "Pasco",
"countrySubdivision": "FL",
"countrySubdivisionName": "Florida",
"countryCode": "US",
"country": "United States",
"countryCodeISO3": "USA",
"freeformAddress": "New Port Richey, FL"
},
"position": {
"lat": 28.2502,
"lon": -82.71408
},
"viewport": {
"topLeftPoint": {
"lat": 28.33939,
"lon": -82.76461
},
"btmRightPoint": {
"lat": 28.17319,
"lon": -82.57478
}
},
"boundingBox": {
"topLeftPoint": {
"lat": 28.33939,
"lon": -82.76461
},
"btmRightPoint": {
"lat": 28.17319,
"lon": -82.57478
}
},
"dataSources": {
"geometry": {
"id": "00005546-3300-3c00-0000-000059a7e5e6"
}
}
},
{
"type": "Geography",
"id": "US/GEO/p0/27115",
"score": 2.414732933,
"entityType": "Municipality",
"address": {
"municipality": "New Bern",
"countrySecondarySubdivision": "Craven",
"countrySubdivision": "NC",
"countrySubdivisionName": "North Carolina",
"countryCode": "US",
"country": "United States",
"countryCodeISO3": "USA",
"freeformAddress": "New Bern, NC"
},
"position": {
"lat": 35.10832,
"lon": -77.04612
},
"viewport": {
"topLeftPoint": {
"lat": 35.28085,
"lon": -77.26051
},
"btmRightPoint": {
"lat": 34.90304,
"lon": -76.83165
}
},
"boundingBox": {
"topLeftPoint": {
"lat": 35.28085,
"lon": -77.26051
},
"btmRightPoint": {
"lat": 34.90304,
"lon": -76.83165
}
},
"dataSources": {
"geometry": {
"id": "00004e43-3300-3c00-0000-0000596be1d9"
}
}
},
{
"type": "Geography",
"id": "US/GEO/p0/60622",
"score": 2.4027762413,
"entityType": "Municipality",
"address": {
"municipality": "New Haven",
"countrySecondarySubdivision": "New Haven",
"countrySubdivision": "CT",
"countrySubdivisionName": "Connecticut",
"countryCode": "US",
"country": "United States",
"countryCodeISO3": "USA",
"freeformAddress": "New Haven, CT"
},
"position": {
"lat": 41.30825,
"lon": -72.92416
},
"viewport": {
"topLeftPoint": {
"lat": 41.35038,
"lon": -72.99807
},
"btmRightPoint": {
"lat": 41.2464,
"lon": -72.81773
}
},
"boundingBox": {
"topLeftPoint": {
"lat": 41.35038,
"lon": -72.99807
},
"btmRightPoint": {
"lat": 41.2464,
"lon": -72.81773
}
},
"dataSources": {
"geometry": {
"id": "00005543-5400-3c00-0000-0000596911a2"
}
}
},
{
"type": "Geography",
"id": "US/GEO/p0/90802",
"score": 2.4020097256,
"entityType": "Municipality",
"address": {
"municipality": "New Iberia",
"countrySecondarySubdivision": "Iberia",
"countrySubdivision": "LA",
"countrySubdivisionName": "Louisiana",
"countryCode": "US",
"country": "United States",
"countryCodeISO3": "USA",
"freeformAddress": "New Iberia, LA"
},
"position": {
"lat": 30.00256,
"lon": -91.81698
},
"viewport": {
"topLeftPoint": {
"lat": 30.12129,
"lon": -91.9944
},
"btmRightPoint": {
"lat": 29.73782,
"lon": -91.65921
}
},
"boundingBox": {
"topLeftPoint": {
"lat": 30.12129,
"lon": -91.9944
},
"btmRightPoint": {
"lat": 29.73782,
"lon": -91.65921
}
},
"dataSources": {
"geometry": {
"id": "0000554c-4100-3c00-0000-00005968eae5"
}
}
},
{
"type": "Geography",
"id": "US/GEO/p0/61589",
"score": 2.3937933445,
"entityType": "Municipality",
"address": {
"municipality": "New Castle",
"countrySecondarySubdivision": "New Castle",
"countrySubdivision": "DE",
"countrySubdivisionName": "Delaware",
"countryCode": "US",
"country": "United States",
"countryCodeISO3": "USA",
"freeformAddress": "New Castle, DE"
},
"position": {
"lat": 39.66066,
"lon": -75.56184
},
"viewport": {
"topLeftPoint": {
"lat": 39.7245,
"lon": -75.66496
},
"btmRightPoint": {
"lat": 39.55219,
"lon": -75.50945
}
},
"boundingBox": {
"topLeftPoint": {
"lat": 39.7245,
"lon": -75.66496
},
"btmRightPoint": {
"lat": 39.55219,
"lon": -75.50945
}
},
"dataSources": {
"geometry": {
"id": "00005544-4500-3c00-0000-000059685073"
}
}
},
{
"type": "Geography",
"id": "US/GEO/p0/36482",
"score": 2.3928732872,
"entityType": "Municipality",
"address": {
"municipality": "New Bedford",
"countrySecondarySubdivision": "Bristol",
"countrySubdivision": "MA",
"countrySubdivisionName": "Massachusetts",
"countryCode": "US",
"country": "United States",
"countryCodeISO3": "USA",
"freeformAddress": "New Bedford, MA"
},
"position": {
"lat": 41.63405,
"lon": -70.92766
},
"viewport": {
"topLeftPoint": {
"lat": 41.74525,
"lon": -70.97843
},
"btmRightPoint": {
"lat": 41.58185,
"lon": -70.87872
}
},
"boundingBox": {
"topLeftPoint": {
"lat": 41.74525,
"lon": -70.97843
},
"btmRightPoint": {
"lat": 41.58185,
"lon": -70.87872
}
},
"dataSources": {
"geometry": {
"id": "0000554d-4100-3c00-0000-00005969fc6d"
}
}
},
{
"type": "Geography",
"id": "US/GEO/p0/12185",
"score": 2.3897461891,
"entityType": "Municipality",
"address": {
"municipality": "New Albany",
"countrySecondarySubdivision": "Floyd",
"countrySubdivision": "IN",
"countrySubdivisionName": "Indiana",
"countryCode": "US",
"country": "United States",
"countryCodeISO3": "USA",
"freeformAddress": "New Albany, IN"
},
"position": {
"lat": 38.28351,
"lon": -85.823
},
"viewport": {
"topLeftPoint": {
"lat": 38.40435,
"lon": -85.8853
},
"btmRightPoint": {
"lat": 38.22944,
"lon": -85.76089
}
},
"boundingBox": {
"topLeftPoint": {
"lat": 38.40435,
"lon": -85.8853
},
"btmRightPoint": {
"lat": 38.22944,
"lon": -85.76089
}
},
"dataSources": {
"geometry": {
"id": "00005549-4e00-3c00-0000-0000596b63cf"
}
}
}
]
}
There is an end point in the Atlas API which might be closer to what we need - Fuzzy search.
It allows to set a typeahead flag and you can filter on entity type, so, for a GET reauest https://atlas.microsoft.com/search/fuzzy/json?api-version=1.0&query=Paris&typeahead=true&limit=3&entityType=Municipality,MunicipalitySubdivision,CountryTertiarySubdivision,CountrySubdivision
with your subscription key in the header
and an example return would be
"summary": {
"query": "paris",
"queryType": "NON_NEAR",
"queryTime": 26,
"numResults": 3,
"offset": 0,
"totalResults": 141,
"fuzzyLevel": 1,
"queryIntent": []
},
"results": [
{
"type": "Geography",
"id": "FR/GEO/p0/13312",
"score": 2.5989630222,
"entityType": "Municipality",
"address": {
"municipality": "Paris",
"countrySecondarySubdivision": "Paris",
"countrySubdivision": "Île-de-France",
"countryCode": "FR",
"country": "France",
"countryCodeISO3": "FRA",
"freeformAddress": "Paris"
},
"position": {
"lat": 48.85689,
"lon": 2.35085
},
"viewport": {
"topLeftPoint": {
"lat": 48.90216,
"lon": 2.2241
},
"btmRightPoint": {
"lat": 48.81552,
"lon": 2.46991
}
},
"boundingBox": {
"topLeftPoint": {
"lat": 48.90216,
"lon": 2.2241
},
"btmRightPoint": {
"lat": 48.81552,
"lon": 2.46991
}
},
"dataSources": {
"geometry": {
"id": "00005858-5800-1200-0000-000077369045"
}
}
},
{
"type": "Geography",
"id": "US/GEO/p0/119280",
"score": 2.4116733074,
"entityType": "Municipality",
"address": {
"municipality": "Paris",
"countrySecondarySubdivision": "Lamar",
"countrySubdivision": "TX",
"countrySubdivisionName": "Texas",
"countryCode": "US",
"country": "United States",
"countryCodeISO3": "USA",
"freeformAddress": "Paris, TX"
},
"position": {
"lat": 33.66143,
"lon": -95.55632
},
"viewport": {
"topLeftPoint": {
"lat": 33.86768,
"lon": -95.66163
},
"btmRightPoint": {
"lat": 33.38955,
"lon": -95.31052
}
},
"boundingBox": {
"topLeftPoint": {
"lat": 33.86768,
"lon": -95.66163
},
"btmRightPoint": {
"lat": 33.38955,
"lon": -95.31052
}
},
"dataSources": {
"geometry": {
"id": "00005554-3100-3c00-0000-0000596a4495"
}
}
},
{
"type": "Geography",
"id": "US/GEO/p0/106765",
"score": 2.4046759605,
"entityType": "Municipality",
"address": {
"municipality": "Paris",
"countrySecondarySubdivision": "Henry",
"countrySubdivision": "TN",
"countrySubdivisionName": "Tennessee",
"countryCode": "US",
"country": "United States",
"countryCodeISO3": "USA",
"freeformAddress": "Paris, TN"
},
"position": {
"lat": 36.30275,
"lon": -88.32636
},
"viewport": {
"topLeftPoint": {
"lat": 36.42548,
"lon": -88.59662
},
"btmRightPoint": {
"lat": 36.18254,
"lon": -88.15962
}
},
"boundingBox": {
"topLeftPoint": {
"lat": 36.42548,
"lon": -88.59662
},
"btmRightPoint": {
"lat": 36.18254,
"lon": -88.15962
}
},
"dataSources": {
"geometry": {
"id": "0000544e-3100-3c00-0000-00005969642b"
}
}
}
]
}
I have a 2d array of country names, C, and a struct type list called countries with one member, country.
if C[i] contains more than 2 words, I want to ignore C[i];
if C[i] contains one word, I want to store that country name C[i] in countries[i].country.
my program is not working. what am I doing wrong?
//
// main.c
// delete 2 words
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <ctype.h>
#include <string.h>
typedef struct list{
char country[50];
}list;
int main(void) {
list countries[100];
int len=0, i=0, j=0;
char C[][100] = {"United States", "Canada", "Afghanistan", "Albania", "Algeria", "American Samoa", "Andorra", "Angola", "Anguilla", "Antarctica", "Antigua and/or Barbuda", "Argentina", "Armenia", "Aruba", "Australia", "Austria", "Azerbaijan", "Bahamas", "Bahrain", "Bangladesh", "Barbados", "Belarus", "Belgium", "Belize", "Benin", "Bermuda", "Bhutan", "Bolivia", "Bosnia and Herzegovina", "Botswana", "Bouvet Island", "Brazil", "British Indian Ocean Territory", "Brunei Darussalam", "Bulgaria", "Burkina Faso", "Burundi", "Cambodia", "Cameroon", "Cape Verde", "Cayman Islands", "Central African Republic", "Chad", "Chile", "China", "Christmas Island", "Cocos (Keeling) Islands", "Colombia", "Comoros", "Congo", "Cook Islands", "Costa Rica", "Croatia (Hrvatska)", "Cuba", "Cyprus", "Czech Republic", "Denmark", "Djibouti", "Dominica", "Dominican Republic", "East Timor", "Ecudaor", "Egypt", "El Salvador", "Equatorial Guinea", "Eritrea", "Estonia", "Ethiopia", "Falkland Islands (Malvinas)", "Faroe Islands", "Fiji", "Finland", "France", "France, Metropolitan", "French Guiana", "French Polynesia", "French Southern Territories", "Gabon", "Gambia", "Georgia", "Germany", "Ghana", "Gibraltar", "Greece", "Greenland", "Grenada", "Guadeloupe", "Guam", "Guatemala", "Guinea", "Guinea-Bissau", "Guyana", "Haiti", "Heard and Mc Donald Islands", "Honduras", "Hong Kong", "Hungary", "Iceland", "India", "Indonesia", "Iran (Islamic Republic of)", "Iraq", "Ireland", "Israel", "Italy", "Ivory Coast", "Jamaica", "Japan", "Jordan", "Kazakhstan", "Kenya", "Kiribati", "Korea, Democratic People's Republic of", "Korea, Republic of", "Kosovo", "Kuwait", "Kyrgyzstan", "Lao People's Democratic Republic", "Latvia", "Lebanon", "Lesotho", "Liberia", "Libyan Arab Jamahiriya", "Liechtenstein", "Lithuania", "Luxembourg", "Macau", "Macedonia", "Madagascar", "Malawi", "Malaysia", "Maldives", "Mali", "Malta", "Marshall Islands", "Martinique", "Mauritania", "Mauritius", "Mayotte", "Mexico", "Micronesia, Federated States of", "Moldova, Republic of", "Monaco", "Mongolia", "Montserrat", "Morocco", "Mozambique", "Myanmar", "Namibia", "Nauru", "Nepal", "Netherlands", "Netherlands Antilles", "New Caledonia", "New Zealand", "Nicaragua", "Niger", "Nigeria", "Niue", "Norfork Island", "Northern Mariana Islands", "Norway", "Oman", "Pakistan", "Palau", "Panama", "Papua New Guinea", "Paraguay", "Peru", "Philippines", "Pitcairn", "Poland", "Portugal", "Puerto Rico", "Qatar", "Reunion", "Romania", "Russian Federation", "Rwanda", "Saint Kitts and Nevis", "Saint Lucia", "Saint Vincent and the Grenadines", "Samoa", "San Marino", "Sao Tome and Principe", "Saudi Arabia", "Senegal", "Seychelles", "Sierra Leone", "Singapore", "Slovakia", "Slovenia", "Solomon Islands", "Somalia", "South Africa", "South Georgia South Sandwich Islands", "South Sudan", "Spain", "Sri Lanka", "St. Helena", "St. Pierre and Miquelon", "Sudan", "Suriname", "Svalbarn and Jan Mayen Islands", "Swaziland", "Sweden", "Switzerland", "Syrian Arab Republic", "Taiwan", "Tajikistan", "Tanzania, United Republic of", "Thailand", "Togo", "Tokelau", "Tonga", "Trinidad and Tobago", "Tunisia", "Turkey", "Turkmenistan", "Turks and Caicos Islands", "Tuvalu", "Uganda", "Ukraine", "United Arab Emirates", "United Kingdom", "United States minor outlying islands", "Uruguay", "Uzbekistan", "Vanuatu", "Vatican City State", "Venezuela", "Vietnam", "Virigan Islands (British)", "Virgin Islands (U.S.)", "Wallis and Futuna Islands", "Western Sahara", "Yemen", "Yugoslavia", "Zaire", "Zambia", "Zimbabwe"};
while (C[i] != NULL){
for (j = 0; j < strlen(C[i]) - 1; ++j){
if (C[i][j] == ' '){
i = i + 1;
}
else if (C[i][j] == '\0'){
i = i + 1;
}
else{
strcpy(C[i], countries[i].country);
i = i + 1;
}
}
}
len = i;
printf("%d", len);
for (i = 0; i < len; ++i){
printf("%s", countries[i].country);
}
return 0;
}
If you scan for a NULL, you need to end your list with a NULL, but you need an array of pointers not a 2D array
You swap the order of the strcpy parameters
Your code is too complicated, the standard library can help you, in this case you can use strchr
#include <stdio.h>
#include <string.h>
typedef struct list
{
char country[50];
} list;
int main(void)
{
list countries[100];
int len = 0;
const char *C[] = {"United States", "Canada", "Afghanistan", "Albania", "Algeria", "American Samoa", NULL};
for (int i = 0; C[i] != NULL; i++)
{
char *ptr = strchr(C[i], ' ');
if (ptr != NULL)
{
strcpy(countries[len++].country, C[i]);
}
}
printf("%d\n", len);
for (int i = 0; i < len; i++)
{
printf("%s\n", countries[i].country);
}
return 0;
}
If the string is not going to be modified, you can also use a pointer for the country member:
#include <stdio.h>
#include <string.h>
typedef struct list
{
char *country;
} list;
int main(void)
{
list countries[100];
int len = 0;
char *C[] = {"United States", "Canada", "Afghanistan", "Albania", "Algeria", "American Samoa", NULL};
for (int i = 0; C[i] != NULL; i++)
{
char *ptr = strchr(C[i], ' ');
if (ptr != NULL)
{
countries[len++].country = C[i];
}
}
printf("%d\n", len);
for (int i = 0; i < len; i++)
{
printf("%s\n", countries[i].country);
}
return 0;
}
The condition of the loop
while (C[i] != NULL){
does not make sense because the array is not an array of pointers,
In this call of strcpy
strcpy(C[i], countries[i].country);
the order of arguments is incorrect.
Pay attention to that in general a string can contain adjacent or leading or trailing blanks. So the approach with using strchr is just bad.
You could write a separate function that will count the number of words in a string.
Here is a demonstrative program.
//
// main.c
// delete 2 words
#include <stdio.h>
#include <string.h>
enum { LENGTH = 50 };
typedef struct list
{
char country[LENGTH];
} list;
size_t count_words( const char *s )
{
const char *delim = " \t";
size_t n = 0;
while ( *s )
{
s += strspn( s, delim );
if ( *s ) ++n;
s += strcspn( s , delim );
}
return n;
}
int main(void)
{
char C[][LENGTH] =
{
"United States", "Canada", "Afghanistan", "Albania", "Algeria",
"American Samoa", "Andorra", "Angola", "Anguilla", "Antarctica",
"Antigua and/or Barbuda", "Argentina", "Armenia", "Aruba",
"Australia", "Austria", "Azerbaijan", "Bahamas", "Bahrain",
"Bangladesh", "Barbados", "Belarus", "Belgium", "Belize", "Benin",
"Bermuda", "Bhutan", "Bolivia", "Bosnia and Herzegovina", "Botswana",
"Bouvet Island", "Brazil", "British Indian Ocean Territory",
"Brunei Darussalam", "Bulgaria", "Burkina Faso", "Burundi", "Cambodia",
"Cameroon", "Cape Verde", "Cayman Islands", "Central African Republic",
"Chad", "Chile", "China", "Christmas Island", "Cocos (Keeling) Islands",
"Colombia", "Comoros", "Congo", "Cook Islands", "Costa Rica",
"Croatia (Hrvatska)", "Cuba", "Cyprus", "Czech Republic", "Denmark",
"Djibouti", "Dominica", "Dominican Republic", "East Timor", "Ecudaor",
"Egypt", "El Salvador", "Equatorial Guinea", "Eritrea", "Estonia",
"Ethiopia", "Falkland Islands (Malvinas)", "Faroe Islands", "Fiji",
"Finland", "France", "France, Metropolitan", "French Guiana",
"French Polynesia", "French Southern Territories", "Gabon", "Gambia",
"Georgia", "Germany", "Ghana", "Gibraltar", "Greece", "Greenland",
"Grenada", "Guadeloupe", "Guam", "Guatemala", "Guinea", "Guinea-Bissau",
"Guyana", "Haiti", "Heard and Mc Donald Islands", "Honduras", "Hong Kong",
"Hungary", "Iceland", "India", "Indonesia", "Iran (Islamic Republic of)",
"Iraq", "Ireland", "Israel", "Italy", "Ivory Coast", "Jamaica", "Japan",
"Jordan", "Kazakhstan", "Kenya", "Kiribati",
"Korea, Democratic People's Republic of", "Korea, Republic of", "Kosovo",
"Kuwait", "Kyrgyzstan", "Lao People's Democratic Republic", "Latvia",
"Lebanon", "Lesotho", "Liberia", "Libyan Arab Jamahiriya",
"Liechtenstein", "Lithuania", "Luxembourg", "Macau", "Macedonia",
"Madagascar", "Malawi", "Malaysia", "Maldives", "Mali", "Malta",
"Marshall Islands", "Martinique", "Mauritania", "Mauritius", "Mayotte",
"Mexico", "Micronesia, Federated States of", "Moldova, Republic of",
"Monaco", "Mongolia", "Montserrat", "Morocco", "Mozambique", "Myanmar",
"Namibia", "Nauru", "Nepal", "Netherlands", "Netherlands Antilles",
"New Caledonia", "New Zealand", "Nicaragua", "Niger", "Nigeria", "Niue",
"Norfork Island", "Northern Mariana Islands", "Norway", "Oman", "Pakistan",
"Palau", "Panama", "Papua New Guinea", "Paraguay", "Peru", "Philippines",
"Pitcairn", "Poland", "Portugal", "Puerto Rico", "Qatar", "Reunion",
"Romania", "Russian Federation", "Rwanda", "Saint Kitts and Nevis",
"Saint Lucia", "Saint Vincent and the Grenadines", "Samoa", "San Marino",
"Sao Tome and Principe", "Saudi Arabia", "Senegal", "Seychelles",
"Sierra Leone", "Singapore", "Slovakia", "Slovenia", "Solomon Islands",
"Somalia", "South Africa", "South Georgia South Sandwich Islands",
"South Sudan", "Spain", "Sri Lanka", "St. Helena", "St. Pierre and Miquelon",
"Sudan", "Suriname", "Svalbarn and Jan Mayen Islands", "Swaziland",
"Sweden", "Switzerland", "Syrian Arab Republic", "Taiwan", "Tajikistan",
"Tanzania, United Republic of", "Thailand", "Togo", "Tokelau", "Tonga",
"Trinidad and Tobago", "Tunisia", "Turkey", "Turkmenistan",
"Turks and Caicos Islands", "Tuvalu", "Uganda", "Ukraine",
"United Arab Emirates", "United Kingdom",
"United States minor outlying islands", "Uruguay", "Uzbekistan", "Vanuatu",
"Vatican City State", "Venezuela", "Vietnam", "Virigan Islands (British)",
"Virgin Islands (U.S.)", "Wallis and Futuna Islands", "Western Sahara",
"Yemen", "Yugoslavia", "Zaire", "Zambia", "Zimbabwe"
};
const size_t M = sizeof( C ) / sizeof( *C );
enum { N = 100 };
list countries[M];
size_t n = 0;
for ( size_t i = 0; i < M && n < N; i++ )
{
if ( count_words( C[i] ) == 1 ) strcpy(countries[n++].country, C[i] );
}
for ( size_t i = 0, j = 0; i < n; i++ )
{
printf( "\"%s\" ", countries[i].country );
if ( ( j = ( j + 1 ) % 4 ) == 0 ) putchar( '\n' );
}
return 0;
}
The program output is
"Canada" "Afghanistan" "Albania" "Algeria"
"Andorra" "Angola" "Anguilla" "Antarctica"
"Argentina" "Armenia" "Aruba" "Australia"
"Austria" "Azerbaijan" "Bahamas" "Bahrain"
"Bangladesh" "Barbados" "Belarus" "Belgium"
"Belize" "Benin" "Bermuda" "Bhutan"
"Bolivia" "Botswana" "Brazil" "Bulgaria"
"Burundi" "Cambodia" "Cameroon" "Chad"
"Chile" "China" "Colombia" "Comoros"
"Congo" "Cuba" "Cyprus" "Denmark"
"Djibouti" "Dominica" "Ecudaor" "Egypt"
"Eritrea" "Estonia" "Ethiopia" "Fiji"
"Finland" "France" "Gabon" "Gambia"
"Georgia" "Germany" "Ghana" "Gibraltar"
"Greece" "Greenland" "Grenada" "Guadeloupe"
"Guam" "Guatemala" "Guinea" "Guinea-Bissau"
"Guyana" "Haiti" "Honduras" "Hungary"
"Iceland" "India" "Indonesia" "Iraq"
"Ireland" "Israel" "Italy" "Jamaica"
"Japan" "Jordan" "Kazakhstan" "Kenya"
"Kiribati" "Kosovo" "Kuwait" "Kyrgyzstan"
"Latvia" "Lebanon" "Lesotho" "Liberia"
"Liechtenstein" "Lithuania" "Luxembourg" "Macau"
"Macedonia" "Madagascar" "Malawi" "Malaysia"
"Maldives" "Mali" "Malta" "Martinique"
As you can see from the program output it seems that the number of elements in the array of structures that is 100 is not enough to accommodate all strings with one word. You should enlarge it. It should have at least 168 elements for the given list of countries.
For example you could at first count the number of strings with one word and then declare a variable length array of structures with the number of elements equal to the found number of strings.
Here is an updated version of the demonstrative program with a variable length array of structures.
//
// main.c
// delete 2 words
#include <stdio.h>
#include <string.h>
enum { LENGTH = 50 };
typedef struct list
{
char country[LENGTH];
} list;
size_t count_words( const char *s )
{
const char *delim = " \t";
size_t n = 0;
while ( *s )
{
s += strspn( s, delim );
if ( *s ) ++n;
s += strcspn( s , delim );
}
return n;
}
int main(void)
{
char C[][LENGTH] =
{
"United States", "Canada", "Afghanistan", "Albania", "Algeria",
"American Samoa", "Andorra", "Angola", "Anguilla", "Antarctica",
"Antigua and/or Barbuda", "Argentina", "Armenia", "Aruba",
"Australia", "Austria", "Azerbaijan", "Bahamas", "Bahrain",
"Bangladesh", "Barbados", "Belarus", "Belgium", "Belize", "Benin",
"Bermuda", "Bhutan", "Bolivia", "Bosnia and Herzegovina", "Botswana",
"Bouvet Island", "Brazil", "British Indian Ocean Territory",
"Brunei Darussalam", "Bulgaria", "Burkina Faso", "Burundi", "Cambodia",
"Cameroon", "Cape Verde", "Cayman Islands", "Central African Republic",
"Chad", "Chile", "China", "Christmas Island", "Cocos (Keeling) Islands",
"Colombia", "Comoros", "Congo", "Cook Islands", "Costa Rica",
"Croatia (Hrvatska)", "Cuba", "Cyprus", "Czech Republic", "Denmark",
"Djibouti", "Dominica", "Dominican Republic", "East Timor", "Ecudaor",
"Egypt", "El Salvador", "Equatorial Guinea", "Eritrea", "Estonia",
"Ethiopia", "Falkland Islands (Malvinas)", "Faroe Islands", "Fiji",
"Finland", "France", "France, Metropolitan", "French Guiana",
"French Polynesia", "French Southern Territories", "Gabon", "Gambia",
"Georgia", "Germany", "Ghana", "Gibraltar", "Greece", "Greenland",
"Grenada", "Guadeloupe", "Guam", "Guatemala", "Guinea", "Guinea-Bissau",
"Guyana", "Haiti", "Heard and Mc Donald Islands", "Honduras", "Hong Kong",
"Hungary", "Iceland", "India", "Indonesia", "Iran (Islamic Republic of)",
"Iraq", "Ireland", "Israel", "Italy", "Ivory Coast", "Jamaica", "Japan",
"Jordan", "Kazakhstan", "Kenya", "Kiribati",
"Korea, Democratic People's Republic of", "Korea, Republic of", "Kosovo",
"Kuwait", "Kyrgyzstan", "Lao People's Democratic Republic", "Latvia",
"Lebanon", "Lesotho", "Liberia", "Libyan Arab Jamahiriya",
"Liechtenstein", "Lithuania", "Luxembourg", "Macau", "Macedonia",
"Madagascar", "Malawi", "Malaysia", "Maldives", "Mali", "Malta",
"Marshall Islands", "Martinique", "Mauritania", "Mauritius", "Mayotte",
"Mexico", "Micronesia, Federated States of", "Moldova, Republic of",
"Monaco", "Mongolia", "Montserrat", "Morocco", "Mozambique", "Myanmar",
"Namibia", "Nauru", "Nepal", "Netherlands", "Netherlands Antilles",
"New Caledonia", "New Zealand", "Nicaragua", "Niger", "Nigeria", "Niue",
"Norfork Island", "Northern Mariana Islands", "Norway", "Oman", "Pakistan",
"Palau", "Panama", "Papua New Guinea", "Paraguay", "Peru", "Philippines",
"Pitcairn", "Poland", "Portugal", "Puerto Rico", "Qatar", "Reunion",
"Romania", "Russian Federation", "Rwanda", "Saint Kitts and Nevis",
"Saint Lucia", "Saint Vincent and the Grenadines", "Samoa", "San Marino",
"Sao Tome and Principe", "Saudi Arabia", "Senegal", "Seychelles",
"Sierra Leone", "Singapore", "Slovakia", "Slovenia", "Solomon Islands",
"Somalia", "South Africa", "South Georgia South Sandwich Islands",
"South Sudan", "Spain", "Sri Lanka", "St. Helena", "St. Pierre and Miquelon",
"Sudan", "Suriname", "Svalbarn and Jan Mayen Islands", "Swaziland",
"Sweden", "Switzerland", "Syrian Arab Republic", "Taiwan", "Tajikistan",
"Tanzania, United Republic of", "Thailand", "Togo", "Tokelau", "Tonga",
"Trinidad and Tobago", "Tunisia", "Turkey", "Turkmenistan",
"Turks and Caicos Islands", "Tuvalu", "Uganda", "Ukraine",
"United Arab Emirates", "United Kingdom",
"United States minor outlying islands", "Uruguay", "Uzbekistan", "Vanuatu",
"Vatican City State", "Venezuela", "Vietnam", "Virigan Islands (British)",
"Virgin Islands (U.S.)", "Wallis and Futuna Islands", "Western Sahara",
"Yemen", "Yugoslavia", "Zaire", "Zambia", "Zimbabwe"
};
const size_t M = sizeof( C ) / sizeof( *C );
size_t n = 0;
for ( size_t i = 0; i < M; i++ )
{
if ( count_words( C[i] ) == 1 ) ++n;
}
list countries[n];
for ( size_t i = 0, j = 0; i < M; i++ )
{
if ( count_words( C[i] ) == 1 ) strcpy(countries[j++].country, C[i] );
}
for ( size_t i = 0, j = 0; i < n; i++ )
{
printf( "\"%s\" ", countries[i].country );
if ( ( j = ( j + 1 ) % 4 ) == 0 ) putchar( '\n' );
}
return 0;
}
Now the program output is
"Canada" "Afghanistan" "Albania" "Algeria"
"Andorra" "Angola" "Anguilla" "Antarctica"
"Argentina" "Armenia" "Aruba" "Australia"
"Austria" "Azerbaijan" "Bahamas" "Bahrain"
"Bangladesh" "Barbados" "Belarus" "Belgium"
"Belize" "Benin" "Bermuda" "Bhutan"
"Bolivia" "Botswana" "Brazil" "Bulgaria"
"Burundi" "Cambodia" "Cameroon" "Chad"
"Chile" "China" "Colombia" "Comoros"
"Congo" "Cuba" "Cyprus" "Denmark"
"Djibouti" "Dominica" "Ecudaor" "Egypt"
"Eritrea" "Estonia" "Ethiopia" "Fiji"
"Finland" "France" "Gabon" "Gambia"
"Georgia" "Germany" "Ghana" "Gibraltar"
"Greece" "Greenland" "Grenada" "Guadeloupe"
"Guam" "Guatemala" "Guinea" "Guinea-Bissau"
"Guyana" "Haiti" "Honduras" "Hungary"
"Iceland" "India" "Indonesia" "Iraq"
"Ireland" "Israel" "Italy" "Jamaica"
"Japan" "Jordan" "Kazakhstan" "Kenya"
"Kiribati" "Kosovo" "Kuwait" "Kyrgyzstan"
"Latvia" "Lebanon" "Lesotho" "Liberia"
"Liechtenstein" "Lithuania" "Luxembourg" "Macau"
"Macedonia" "Madagascar" "Malawi" "Malaysia"
"Maldives" "Mali" "Malta" "Martinique"
"Mauritania" "Mauritius" "Mayotte" "Mexico"
"Monaco" "Mongolia" "Montserrat" "Morocco"
"Mozambique" "Myanmar" "Namibia" "Nauru"
"Nepal" "Netherlands" "Nicaragua" "Niger"
"Nigeria" "Niue" "Norway" "Oman"
"Pakistan" "Palau" "Panama" "Paraguay"
"Peru" "Philippines" "Pitcairn" "Poland"
"Portugal" "Qatar" "Reunion" "Romania"
"Rwanda" "Samoa" "Senegal" "Seychelles"
"Singapore" "Slovakia" "Slovenia" "Somalia"
"Spain" "Sudan" "Suriname" "Swaziland"
"Sweden" "Switzerland" "Taiwan" "Tajikistan"
"Thailand" "Togo" "Tokelau" "Tonga"
"Tunisia" "Turkey" "Turkmenistan" "Tuvalu"
"Uganda" "Ukraine" "Uruguay" "Uzbekistan"
"Vanuatu" "Venezuela" "Vietnam" "Yemen"
"Yugoslavia" "Zaire" "Zambia" "Zimbabwe"