检查数组中是否包含字符串,如果不包含,则附加(C)

时间:2020-07-28 11:19:10

标签: arrays c loops unique c-strings

我有2个数组,一个叫做“ edges”,其中包含一个城市名称列表,另一个叫“ cityNames”,它初始化为一个空字符串。

我想做的是逐个遍历edges数组,然后查看它是否包含在cityNames数组中。如果是,则移至边缘的下一个元素;如果不是,则将值附加到cityNames数组。

下面的代码将edge [i] .startCity添加到cityNames数组,但是它不检查重复项,我不知道为什么。

for (int i = 1; i < noEdges; i++) {
        for (int j = 0; j < noCities; j++) {
            if(strcmp(edges[i].startCity, cityNames[j].cityName) != 0) {
                strcpy(cityNames[i].cityName, edges[i].startCity);
            }
        }
        noCities += 1;
    }

预先感谢

3 个答案:

答案 0 :(得分:1)

我认为:

  • edges是已知长度noEdges的结构的数组,每个结构包含一个 string (一个char指针或一个char数组)
  • cityNames是一个结构数组,其大小至少为不同名称的数量(可以是noEdgesedges数组的大小)
  • cityNames结构包含一个char数组元素,其大小至少为最长名称+ 1(对于终止null为+1)

然后下面的代码可以给出唯一的名称:

noCity = 0;
for (int i = 0; i < noEdges; i++) {
        int dup = 0;       // expect edges[i].startCity not to be a duplicate
        for (int j = 0; j < noCities; j++) {
            if(strcmp(edges[i].startCity, cityNames[j].cityName) == 0) {
                dup = 1;   // got a duplicate
                break;     // no need to go further ...
            }
        }
        if (dup == 0) {    // not a duplicate: add it to cityNames
            strcpy(cityNames[noCities].cityName, edges[i].startCity);
            noCities += 1; // we now have one more city
        }
    }
}

答案 1 :(得分:0)

一开始的一个好主意是,如果可以的话,放弃使用字符串(或至少在实际需要时操纵字符串)。

您可以通过为每个城市名称分配一个数字来开始,这样您就可以拥有一个更快速,更轻松地使用的整数数组。 扫描重复项变得很简单,因为您现在只需要比较数字即可。

当需要在屏幕上显示实际文本或将城市名称写入文件时,可以使用与城市名称关联的索引来检索索引的适当文本表示形式。然后,您可以将cityNames []的数据类型替换为int。这使得每个“边”的“边”连接一个数字而不是文本。

char* actualCityNames[n]; //array holding all city names with duplicates, could be a file also
char* indexedCityNames[n];//array with indexed cities (in order of appearance in actualCityNames, i.e. not alphabetical order)
//indexedCityNames will most likely not use up N slots if duplicates occur
//this is why there is a second counter for the size of indexed cities

int indexedCount = 0;//number of unique city names
int duplicates = 0;

//loop for actualCityNames slots
for(int i=0; i<n; i++){
    
    //loop for indexedCityNames
    for(int j=0; j<indexedCount; j++){
        
        //strcmp returns 0 if both strings are the same
        if(strcmp(actualCityNames[i],indexedCityNames[j]) == 0){
            //duplicate found, mark flag
            duplicates = 1;
        }
    } 
    
    if(!duplicates){
        strcpy(indexedCityNames[indexedCount],actualCityNames[I]);
        indexedCount++;
    }
    duplicates = 0;
}

答案 2 :(得分:0)

您的代码段不检查重复项,因为在内部循环中,一旦遇到不等于当前startCity的第一个cityName,if语句将追加startCity

此外,此声明中

strcpy(cityNames[i].cityName, edges[i].startCity);
                ^^^

使用了错误的索引。

仅当附加了新的noCities时,变量startCity才应递增。

此外,外循环应从等于0的索引开始。

通过以下方式重写循环

int noCities = 0;

for ( int i = 0; i < noEdges; i++ ) {
    int j = 0;

    while ( j < noCities && strcmp(edges[i].startCity, cityNames[j].cityName) != 0 ) {
        ++j;
    }
        
    if ( j == noCities ) strcpy(cityNames[noCities++].cityName, edges[i].startCity);
}
相关问题