updated, done for now? I fixed your int array loop .. still needs a little more work. I left in the printf's so you can see what is going on.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define WIDTH 50
int main()
{
/**/
char **wordarr, **wordpc;
int option=1, library, howmany, i,number, numwords=0,count=0;
//open the file
FILE *fp;
fp = fopen("test", "r");
if (fp == NULL) {
fprintf(stderr, "File not found\n");
exit(1);
}
//counts the words in the file
int ch, letterCount = 0;
while((ch = fgetc(fp)) != EOF){
if(ch == ' ' || ch == '\n'){
count++;
}
else if(ch == ',' || ch == '.' || ch == '!' || ch == '?');
else
letterCount +=1;
}
printf("letterCount %d\ncount %d\n", letterCount, count);
// INCRESED COUNT BY 1 FOR SAFE KEEPING.
// IT WAS CAUSING A SEG FAULT BY NOT ADDING ONE TO IT
//making the array
wordarr = (char**)malloc(sizeof(char*) * count+1);
for(i = 0; i<count+1; i++){
wordarr[i] = (char*)malloc(sizeof(char) * WIDTH);
}
//
//putting the words into the array
char buffer[10000];
fp = fopen("test", "r");
char *tok;
i=0;
// char TwoD[30][30];
while( fgets(buffer, 10000, fp) != NULL){
int len = strlen(buffer);
if (len > 0 && buffer[len-1] == '\n') {
buffer[--len] = '\0';
}
tok = strtok(buffer, " .,!");
while (tok != NULL) {
int len = strlen(tok);
for ( int d = 0; d < len; d++)
{ // CHAGED IT TO MAKE IT COPY PROPERLY INTO YOUR
// 2D ARRAY
wordarr[i][d] = tok[d];
}
i++;
tok = strtok(NULL, " .,!");
}
}
fclose(fp);
int j=0;
for (j=0; j<i; j++) {
printf("[%s]\n", wordarr[j]);
}
////////////////
printf("\nWrite how many letters does the word have?\n");
scanf("%d", &number);
int len;
//goes to the first array and searches for words with NUM characters
//stores the words to an array
//make a static so it can hold the position of where the word is so
//I can copy it to the other array
int stat_position[20];
int move1 = 0;
// printf("count %d sizeof %lu\n", count, sizeof(wordarr)/sizeof(wordarr[0][0]) );
for(i=0; i<count; i++)
{
len=0;
len = strlen(wordarr[i]);
// printf("i = %d\n",i);
if( len == number)
{
// printf("len %d i= %d\n", len, i);
numwords++;
//only move that int array element by one if len found
// to keep what element number the wordarr is inside of it
// and not put the proper number element of your wordarr
// in the same int element number
stat_position[move1]=i;
printf("move1 %d word is %s\n", move1, wordarr[i]);
move1++;
}
}
//ALLOCATE 2DARRAY
wordpc = (char**)malloc(sizeof(char*) * numwords);
for(i = 0; i<numwords; i++){
wordpc[i] = (char*)malloc(sizeof(char) * WIDTH);
}
printf("\n");
for (int h = 0; h < numwords ; h++)
printf("%d\n", stat_position[h]);
printf("\n");
// now you have your int array with the proper
// amount of elelments holding the peoper number
// of which element number wordarr has the
// chosen words in it by length
for (int d = 0; d < move1; d++)
strcpy(wordpc[d], wordarr[stat_position[d]]);
j=0;
printf("count %d sizeof %lu\n", count, sizeof(wordpc)/sizeof(wordpc[0]) );
for (j=0; j<numwords; j++) {
// if lenght matches the number requested
// print it out to screen
// int len = strlen(wordpc[j]);
// if ( len == number)
printf("[fg %s]\n", wordpc[j]);
}
for (j=0; j<sizeof(wordpc)/sizeof(wordpc[0]); j++) {
// if lenght matches the number requested
// print it out to screen
int len = strlen(wordpc[j]);
if ( len == number)
printf("[%s]\n", wordpc[j]);
}
//Deallocate the memory
for (i=0; i<count; i++) {
free(wordarr[i]);
// free(wordpc[i]);
}
free(wordpc); // you need use / get the proper count for its size to free it if you want to do that in a loop
return 0;
}