Trouble with some string array logic

Basically what I am trying to do is take a 50 by 50 by 21 array of strings(line by word, by character) and sort them into a new 2 dimensional array with rows being unique words and columns being characters. The only problem I can't seem to figure out is the logic to pull this off.

In the following code, everything works except the function toWord(), which has the the logic I've tried in there. This is really bothering me. :/ Any help would be greatly appreciated. Thanks!


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include <stdio.h>
#include <string.h>



//Variables
FILE *fo, *fp;
int line, row, col, ch, row2, col2;
char  inFileName[101], outFileName[105], streamin[50000], word[21], wordArray[2501][21], textArray[50][50][21], *readPoint=streamin;

//Prototypes
void openInFile(char inFileName[]);
void openOutFile(char outFileName[]);
void reader(char inFileName[]);
void debug(void);
void toWord(void);

int main(void)
{
//takes in input file and will repeat until file is able to be opened
do
 {

   //take in file name, call it inFileName
   printf("\nPlease enter the name of the file you wish to use: ");
   fgets(inFileName, sizeof(inFileName)-1, stdin);

   //removes newline character taken in by fgets
     for(row=0;row<101;row++)
      if(inFileName[row]=='\n')
       inFileName[row]='\0';

   //attempts to open file
   openInFile(inFileName);
   if(fp==NULL)
    printf("\nFile cannot be opened, please try again.");
 }while(fp==NULL);



    //copy and add ".out" to file name and make outFileName
    strncpy(outFileName, inFileName, sizeof(inFileName));
    strcat(outFileName, ".out");

    //read input file, and split into textArray.
    reader(inFileName);

    //Optional debug function here
    //debug();

    toWord();


return 0;
}

//Function for opening input file
void openInFile(char inFileName[])
{
 fp=fopen(inFileName, "r");
}

//Function for opening output file
void openOutFile(char outFileName[])
{
fo=fopen(outFileName, "r+");
}

//Function to turn input file into textArray
void reader(char inFileName[])
{


//Read file and turn into big string
while((ch=fgetc(fp)) != EOF)
{

 streamin[col]=ch;
 col++;

}
fclose(fp);

//Turn string into big array. If \n is encountered, new line. Improprer entries removed and only words remaining
row=0;
line=0;
for(row=0;row<51;row++)

 for(col=1;col<22;col++)
 {
  if(*readPoint=='\n')
  {
   ++readPoint;
   line++;
   row=0;
   break;
  }


  if(*readPoint==' '
   ||*readPoint<=64
   ||*readPoint>=123
   ||*readPoint==91
   ||*readPoint==92
   ||*readPoint==93
   ||*readPoint==94
   ||*readPoint==95
   ||*readPoint==96)
    {
     ++readPoint;
     break;
    }

  textArray[line][row][col]= *readPoint;
  ++readPoint;
 }

}


//Print out reformatted data in textArray
void debug(void)
{
for(line=0;line<51;line++)
 for(row=0;row<51;row++)
  for(col=1;col<22;col++)
   if(textArray[line][row][col]!='\0' && textArray[line][row][col]!='\n' && textArray[line][row][col]!=' ')
    printf("%c", textArray[line][row][col]);
}



//Problem Is here, cannot wrap my head around this logic

void toWord(void)
{
 for(row=0;row<20;row++)
{
for(col=0;col<22;col++)
 word[col]=textArray[line][row][col];


for(row2=0;row2<25;row++)
 for(col2=0;col2<22;col++)
  if(word[col2]!=wordArray[row][col2])
  {
     wordArray[row2][col]=word[col];
  }
  else
  {
      break;
  }


}

//Trying to print out unique word list here
for(row2=0;row2<2501;row2++)
 for(col=0;col<22;col++)
  printf(" %d%d %c", row2,col,wordArray[row2][col]);

}



Is toWord supposed to map strings form textArray to wordArray ?
Either
1
2
3
for( int i = 0; i < 50; i++ )
   for( int j = 0; j < 50; j++ )
      strcpy( wordArray[i+50*j], textArray[i][j] );
or
1
2
for( int i = 0; i < 2500; i++ )
   strcpy( wordArray[i], textArray[i/50][i%50] );
You could even
memcpy( wordArray, textArray, sizeof( wordArray ) );
though this isn't a great approach as it isn't obvious that it works.

If you want to sort the array (or was that a typo?) do it in a separate function. Look up bubble sort and strcmp.

Off topic, you didn't get a reply for several days mostly because large amounts of code are discouraging. Next time try posting only the most relevant parts.
Last edited on
Topic archived. No new replies allowed.