[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 265
  • Last Modified:

Why wont these functions work?

What is wrong with this function?
I was told that this function only inputted garbage and ai thought I understood but after trying all night to figure it out I am still wher I began.  Please tell me where to go.  I am trying to compare strings from a text  file to an array of store words and add to arry if unique or get next word if duplicate.  ai am also trying to eliminate cade discriminants and punctuation added
duplicates.  Here is the function I need help with.

int insertWord(char list[][MAX_SIZE], FILE *fp){
   
    int count = 0, i=0;  
    char word[30];  
   
   
    while (fscanf(fp,"%s",list[i])!=EOF)i++ ;
   
     for (i=0; word[i]; i++) {
             if (!isalpha(word[i])) {
                 word[i] = 0;  
                 break;
             }
     }
     

        if (!foundWord(list, word,count))  
            strcpy (list[count++], word);      
             return count;
}


int  foundWord(char list[][MAX_SIZE],char word[], int size){

     int i;

     for(i=0;i<size;i++)
         if (stricmp(list[i],word)==0) return 1;
         return 0;
}





0
jholmes9186
Asked:
jholmes9186
  • 4
  • 3
  • 3
  • +1
1 Solution
 
jholmes9186Author Commented:
Sorry, the above should say case discriminants.
0
 
Sjef BosmanGroupware ConsultantCommented:
Hi jholmes9186,

You should explain to an invisble listener what the function does.

>     while (fscanf(fp,"%s",list[i])!=EOF)i++ ;
This reads strings into the list[] array

>      for (i=0; word[i]; i++) {
>              if (!isalpha(word[i])) {
>                  word[i] = 0;  
>                  break;
>              }
>      }
This removes non-alpha words or so??? word[] hasn't been used yet!
 
>         if (!foundWord(list, word,count))  
>             strcpy (list[count++], word);
Beats me what's the purpose...
     
>             return count;
Indentation wrong, but the final count is returned.

Somehow, I assume, word[] used to be filled, but did you remove that code?

Cheers!
   Sjef
0
 
KurtVonCommented:
Well, the variable "word" is never initialized.  So whatever block of memory is in it will be used for the search.

There's also the problem that the variable found may not be large enough to hold the file data.

Finally, the value of count is never initialized, so the find never even looks at a single element of the array, and will always fail and then copy the stuff in "word" (likely empty) over the first element in the array.

Hope this helps.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
sunnycoderCommented:
usr char * [MAX_LIST] and allocate memory for each char * if you want this to work

Instead of char list[ROW][COLS], use
char * list [ROWS];

for ( int i=0; i<ROWS; i++ )
{
           list[i] = (char *) malloc (COLS);
           if ( list[i] == NULL )
           {
                   printf ("out of memory\n");
                   exit(1);
           }
}
0
 
jholmes9186Author Commented:
Sorry, I will clarify:

The first function insertWord is to read strings from an input txt file and calls foundWord to add the word being read to the array word and compare it with the list of unique words in list[][].  It should eliminate case variants as appearing to be separate words  (ie case and Case should be read as the same word) and also eliminate words with punctuation variants as being different words (ie word and word? should be read as the same word).  There is also a counter to display the total number of unique words.
0
 
KurtVonCommented:
In that case, the first "while" needs ot encompass the entire function (short of the return).  It should read teh file data into the word variable, and then eliminate alphanumerics from word and call the foundWord function to find duplicates, finally adding the word if not found.

That should do it, assuming the list variable can fit all the strings.

0
 
sunnycoderCommented:
nope Kurt,
foundWord traverses the entire list

     for (i=0; word[i]; i++) {
             if (!isalpha(word[i])) {  << word array is uninitialized ... you need to read in something into this array before you start checking
                 word[i] = 0;  
                 break;
             }
     }
     

        if (!foundWord(list, word,count))   << this call to foundWord should pass count as number of elements in the list ... currently you are passing 0 .. replacing i with count in the first while loop should take care of this
            strcpy (list[count++], word);      
            return count;
}
0
 
sunnycoderCommented:
oops you are right even the file can have duplicates

int insertWord(char list[][MAX_SIZE], FILE *fp){
   
    int count = 0, i=0;flag=0;  
    char word[30];  
   
   
    while (fscanf(fp,"%s",word)!=EOF) {    
         for (i=0; word[i]; i++) {
                 if (!isalpha(word[i])) {
                     word[i] = 0;  
                      flag=1;
                      break;
                 }
         }
         if ( flag )
         {
                   flag =0;
                   continue;
         }
        if (!foundWord(list, word,count))  
            strcpy (list[count++], word);      
}


int  foundWord(char list[][MAX_SIZE],char word[], int size){

     int i;

     for(i=0;i<size;i++)
         if (stricmp(list[i],word)==0) return 1;
         return 0;
}
0
 
jholmes9186Author Commented:
Thanks for the help.  i have made the changes you suggested and tried to compile it.  I have come up with several errors.  It says that size, word, number are undefined identifiers.  It also gives a syntax error.  Here is the whole code.  Did I misunderstand something or does the rest of my program change the functions incorrectly??

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#define MAX_SIZE 30

int  foundWord(char list [][MAX_SIZE],char word[],int size);
int insertWord(char list [][MAX_SIZE],FILE*fp);

int main(void) {

char list[1000][MAX_SIZE];
int wordcount;

FILE*fp;
fp=fopen("pgm6.txt","r");
if(fp==NULL){
      printf("****ERROR**** input file cannot be read");
      exit (1);
      }
      
wordcount=insertWord(list,fp);

fclose(fp);
      
      return 0;
}

int insertWord(char list[][MAX_SIZE], FILE *fp){
   
    int count = 0, i=0;flag=0;  
    char word[30];  
   
   
    while (fscanf(fp,"%s",word)!=EOF) {    
         for (i=0; word[i]; i++) {
                 if (!isalpha(word[i])) {
                     word[i] = 0;  
                      flag=1;
                      break;
                 }
         }
         if ( flag )
         {
                   flag =0;
                   continue;
         }
        if (!foundWord(list, word,count))  
            strcpy (list[count++], word);      
}


int  foundWord(char list[][MAX_SIZE],char word[], int size){

     int i;

     for(i=0;i<size;i++)
         if (stricmp(list[i],word)==0) return 1;
         return 0;
}


0
 
Sjef BosmanGroupware ConsultantCommented:
Is this homework after all?
0
 
KurtVonCommented:
You're misisng a } at the end of insertWord.  You also need to return count at the end of insertWord.
0
 
sunnycoderCommented:
wow ?
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 3
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now