Ok so for extra credit in my software/hardware design class I have to locate duplicate entries in a defined string matrix. I am really close but having trouble. Case 3 is where I am trying to acomplish the goal. Any tips? My problem is that while it will print duplicates, it also prints out other cities that are not duplicates. Thanks.
#include<stdio.h>
#include<string.h>
int main(void) {
int i;
int k;
int choice, entry, entry1;
int max_array = 14;
char temp[20];
char city_names[15][20] = {
"Los Angeles",
"New York",
"Miami",
"Chicago",
"Atlanta",
"Las Vegas",
"Orlando",
"Washington",
"Miami",
"Houston",
"New Orleans",
"San Francisco",
"Chicago",
"San Diego",
"X"};
printf("INput list of cities is: ");
for(i=0; strcmp("X" , city_names[i]); i++) {
printf("%s\n" , city_names[i]);
}
while(1) {
printf("\nPlease select form the menu below:\n");
printf("1. Print details of strign array entry.\n");
printf("2. Swap two entries in the string array.\n");
printf("3. Find and remove duplicate entries.\n");
printf("4. Exit\n");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("You chose option %d" , choice);
while(1){
printf("\nPlease enter entry of array:\n");
scanf("%d" , &entry);
if(entry > max_array)
printf("Entry is out of bounds\n");
else{
break;
}
}
printf("Entry %d contains %s , which is %d characters long.\n" , entry, city_names[entry] , strlen(city_names[entry]));
break;
case 2:
printf("You chose option %d" , choice);
while(1){
printf("\nPlease enter number to switch: \n");
scanf("%d %d" , &entry , &entry1);
if(entry > max_array || entry1 > max_array)
printf("Entry out of bounds\n");
else {
break; }
}
strcpy(temp, city_names[entry]);
strcpy(city_names[entry], city_names[entry1]);
strcpy(city_names[entry1], temp);
printf("Result list of cities is: \n");
for(i=0; strcmp("X" , city_names[i]); i++) {
printf("%s\n" , city_names[i]);
}
break;
case 3:
for(k=0; k<= max_array; k++) {
for(i=0; i<= max_array; i++){
if(strcmp(city_names[k],city_names[i+1]) == 0) {
printf("%s" , city_names[k]);
}
}
}
break;
case 4:
return 0;
default:
printf("Try again");
}
}