This is my main.c
#include <stdio.h>
#include "fileIO.h"
int main() {
char array1[MAX_LINES][MAX_CHAR] = {0};
char array2[MAX_LINES][MAX_CHAR] = {0};
int occurrences[MAX_LINES];
readSentences("..//inputFiles/input1.txt", array1);
readSentences("..//inputFiles/input2.txt", array2);
insertionSort(array2);
insertionSort(array1);
for (int i = 0; i < MAX_LINES; i++) {
printf("%s", array2[i]);
}
}
This is the code for reading the .txt file into the array
void readSentences(char inputFilePath[], char inputSentences[][MAX_CHAR]) {
const char *INPUT_FILE_PATH = inputFilePath;
FILE *fp = fopen(INPUT_FILE_PATH, "r+");
int lineNum = 0;
while (fgets(inputSentences[lineNum], sizeof(inputSentences[lineNum]), fp) != NULL) {
lineNum++;
}
fclose(fp);
}
This is the code I'm using to sort the 2D array of strings
void insertionSort(char inputStrings[][MAX_CHAR]) {
for (int i = 0; i < MAX_LINES; i++) {
int j = i;
while (j > 0 && strcasecmp(inputStrings[j], inputStrings[j-1]) < 0) {
swap(inputStrings, j, j - 1);
j--;
}
}
}
void swap(char array[MAX_LINES][MAX_CHAR], int index1, int index2) {
if (index1 != index2) {
char swap_elem[MAX_CHAR];
memset(swap_elem, '\0', sizeof(swap_elem));
strcpy(swap_elem, array[index1]);
strcpy(array[index1], array[index2]);
strcpy(array[index2], swap_elem);
}
}
This is the output I'm getting when printing the string
Australia
Dominican Republic
Ecuador
Hong Kong
Hong Kong
Hong Kong
Hong Kong
Italy
Republic of IrelandTaiwan
Taiwan
Ukraine
Notice how Republic of Ireland and Taiwan are concatenated This is the output when the other array
Australia
Dominican Republic
Ecuador
Hong Kong
Hong Kong
Hong Kong
Hong Kong
Italy
Republic of IrelandTaiwan
Taiwan
Ukraine
Notice how this time its Republic of Ireland and Taiwan which have concatenated