Program to sort country names in C

Program to sort country names in C



#include
#include

#define countryNo 5
#define maxLength 100

void sortNames(char country[countryNo][maxLength])
{
char temp[maxLength];
int i, j;

// bubble sort
for (j = 0; j < countryNo - 1; j++)
{
for (i = 0; i < countryNo - 1; i++)
{
if (strcmp(country[i], country[i + 1]) > 0)
{
strcpy(temp, country[i]);
strcpy(country[i], country[i + 1]);
strcpy(country[i + 1], temp);
}
}
}
}


int main(int argc, char **argv)
{
char country[countryNo][maxLength];
int i;

// enter names
for (i = 0; i < countryNo; i++)
{
printf("Enter name of country #%d: ", i + 1);
gets(country[i]);
}

sortNames(country);

// display
for (i = 0; i < countryNo; i++)
{
printf("%s\n", country[i]);
}

return 0;
}



1 comment:

  1. it is very difficult coding, something changes in this program to easy to understand

    ReplyDelete