0

我有以下两个功能。如何更改代码以使其按字母显示?

例如,如果我定义“A”,它将只显示以 A 开头的AllRecords。

char* displayRecordContent (CountryRecordType ctryRec)
{
  char * output;
  output = ctryRec.Country;
  return output;
}

// ====================================================================

void showAllRecords ()
{
  int i=0;
  char * result;
  for (i=0; i<NoOfRecordsRead; i++)
  {
    result = displayRecordContent (globalCountryDataArray [i]);
    printf ("(%d) %s\n", i, result);
  }
}

输出:

(0) Andorra
(1) United Arab Emirates
(2) Afghanistan
(3) Antigua and Barbuda
(4) Anguilla
(5) Albania
(6) Armenia
(7) Netherlands Antilles
(8) Angola
(9) Antarctica
4

2 回答 2

2

那是你要的吗?

void showAllRecords ()
{
  int i=0;
  char * result;
  for (i=0; i<NoOfRecordsRead; i++)
  {
    result = displayRecordContent (globalCountryDataArray [i]);
    if(*result == 'A') // or other letter
      printf ("(%d) %s\n", i, result);
  }
}
于 2012-08-05T09:53:54.720 回答
2
void showAllRecords (char begin)
{
  int i=0, j=0;
  char * result;
  for (i=0; i<NoOfRecordsRead; i++)
  {
    result = displayRecordContent (globalCountryDataArray [i]);
    if (result[0] == begin) {
        printf ("(%d) %s\n", j, result);
        j++;
    }
  }
}
于 2012-08-05T09:54:03.740 回答