Rahel Miz
Rahel Miz

Reputation: 159

how to check if a 2d array of strings contains more than 1 word?

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;
}
    

Upvotes: 0

Views: 80

Answers (2)

Vlad from Moscow
Vlad from Moscow

Reputation: 310950

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" 

Upvotes: 0

David Ranieri
David Ranieri

Reputation: 41017

  1. 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

  2. You swap the order of the strcpy parameters

  3. 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;
}

Upvotes: 2

Related Questions