I'm passing a 2D array as an argument. Reviewing the array in main shows that I have 4 separate strings of words each being stored in words[1][], words[2][], etc. However, when I pass that array to the function I am using to append plural endings, I only seem to be able to access the first string in the array.
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define ROWS 50
#define COLS 50
void readFileFormat(char words[ROWS][COLS]);
void determinePlurals(char words[ROWS][COLS]);
int main()
{
char words[ROWS][COLS];
readFileFormat(words);
determinePlurals(words);
return 0;
}
void readFileFormat(char words[ROWS][COLS])
{
int cols = 0;//consistent cols for 2d array
FILE* nounFile;
fopen_s(&nounFile, "plural.txt", "r");
if (nounFile == NULL)
{
printf("Error reading file; try again.");
return 1;
}
while (!feof(nounFile))
{
for (int rows = 0; rows < ROWS; ++rows)
{
fgets(&words[rows][cols], 50, nounFile);
words[rows][strcspn(words[rows], "\n")] = 0;
}
}
fclose(nounFile);
}
void determinePlurals(char words[ROWS][COLS])
{
char* iesEnd = "ies";
char* esEnd = "es";
char* sEnd = "s";
for (int rows = 0; rows < ROWS; ++rows)
{
if (strcmp(words[rows] + strlen(words[rows]) - 1, "y") == 0)
{
printf("%s: ", words);
words[rows][strlen(words[rows]) - 1] = '\0';
strncat(words[rows], iesEnd, 3);
printf("%s\n", words);
rows++;
}
else if (strcmp(words[rows] + strlen(words[rows]) - 2, "ch") == 0
|| strcmp(words[rows] + strlen(words[rows]) - 2, "ss") == 0
|| strcmp(words[rows] + strlen(words[rows]) - 2, "x") == 0
|| strcmp(words[rows] + strlen(words[rows]) - 2, "sh") == 0)
{
printf("%s: ", words);
strncat(words[rows], esEnd, 2);
printf("%s\n", words);
rows++;
}
else
{
printf("%s: ", words);
strncat(words[rows], sEnd, 1);
printf("%s\n", words);
rows++;
}
}
}
I've tried iterating through each row, but don't seem to have acccess to the rest of words[][]. I've struggled with multidimensional arrays so I'm looking for a bit of assistance.