I am trying to compare strings that I have gotten from a struct Array and am trying to see if both are equal before I print it out.
int index;
for (int i = 0; strlen((cityArr+i)->cityname) != 0; i++) {
for (int j = 0; strlen((cityArr+j)->cityname) != 0; j++) {
if (strcmp(cityArr[i].cityname, cityArr[j].cityname) == 0) {
index = i;
}
}
}
printf("%s\n", cityArr[index].cityname);
So the information I have basically means that I should just print a duplicate right?
However, my output is: San Jose Fort Worth San Diego Pittsburgh Omaha Stockton Austin New York Corpus Christi Fort Worth
I believe that the only city that should be printed is Fort Worth, not all the cities (which is the case here).
Someone identified my question as a duplicate - I read through the topic, but I somewhat understand how does strcmp work. strcmp is returns a value of 0 if the strings are equal, but here I am trying to print out the equal city names, but instead it prints out every city in the array I am working on.