Learn how to a build a cloud-first strategyRegister Now

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

READING AND COMPARING STRINGS

I was workig on this code yesterday.  THe goal is to read a etxt file (I am using a song lyrics file).  I want to read each string and then compare it to a list of unique words (elimnate case variations and trailing punctutation).  I have tried to get this to compile and after spending allnight I am not sue where to go next.

Here are the two functions I cannot get to work.

int insertWord(char list [][30],FILE*fp);
int insertWord(char list [][30],FILE*fp){
int count=0, i=0;
char buffer[30];

while fscanfa9fp,"%s",list[i])!=EOF)i++;

for(i=0;buffer[i];i++){
  if(!isalpha(buffer[i])){
  buffer[i]=0;
  break;
}
}
if(!foundWord(list,buffer,count))
 strcpy (list[count++],buffer);
}return count;
}


after these I will printf the number of unique words and the list of words.  I just get a black screen flashing and no words.  The code looks like it should work but doesn't.??????



int foundWord(char list[][30], char word[], int size);
int foundWord(char list[][30], char word[], int size){
int i=0;

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

0
jholmes9186
Asked:
jholmes9186
  • 6
  • 3
  • 3
1 Solution
 
sunnycoderCommented:
can you post the errors you faced ...

also, a 2D char array is not necessarily same as an array os strings .. for a detailed explanation
http://www.experts-exchange.com/Programming/Programming_Languages/C/Q_20823846.html
0
 
sunnycoderCommented:
I feel the array representation is the main bug in the code ... You need to modify your code a bit according to  the comments posted at the link I gave
0
 
jholmes9186Author Commented:
Thanks for the suggestion but I hate to admit that the explantion is still a bit beyond my knowledge level to comprehend.  I will post the whole code Ihave written below.  I am getting to the point where I am trying to program beyond my level.  If you look at my code does it seem sensible.  I tried to do it b writing an algorithm and then writing the corresponding code but there are several areas I just kind of "brain-locked" on.  The specific errors I am getting are listed below in comment braces.  THe compiler I am using is Codewarrior if that makes a difference.  Also I am trying to get a grip on the flow of code.

#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][30];

     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[][MAat I did here.*/*error here about buffer not being intialized before use.  I thought that was what I did here.*/
   
   
    while (fscanf(fp,"%s",list[i])!=EOF)i++ ;
   
     for (i=0; buffer[i]; i++) {
             if (!isalpha(buffer[i])) {
                 buffer[i] = 0;  
                 break;
             }
     }
     

        if (!foundWord(list, buffer,count))  /* Compiler error here about change of form.  I added the count and the error cleared but I think the logic is wrong*/
            strcpy (list[count++], buffer);      
    }
    return count; ?* declaration syntax  error*/
} /*declaration sysntax error/


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

     int i;

     for(i=0;i<size;i++)
         if (stricmp(list[i],word)==0) return1: /* compiler doesn't recognize this code (I thought it meant compare without regard to letter case*/
         return 0;
}

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
jholmes9186Author Commented:
I am not sure if my previous post went anywhere so I am posting it  again.  If it is a dulicate, sorry1

Thanks for the suggestion but I hate to admit that the explantion is still a bit beyond my knowledge level to comprehend.  I will post the whole code Ihave written below.  I am getting to the point where I am trying to program beyond my level.  If you look at my code does it seem sensible.  I tried to do it b writing an algorithm and then writing the corresponding code but there are several areas I just kind of "brain-locked" on.  The specific errors I am getting are listed below in comment braces.  THe compiler I am using is Codewarrior if that makes a difference.  Also I am trying to get a grip on the flow of code.

#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][30];

     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[][MAat I did here.*/*error here about buffer not being intialized before use.  I thought that was what I did here.*/
   
   
    while (fscanf(fp,"%s",list[i])!=EOF)i++ ;
   
     for (i=0; buffer[i]; i++) {
             if (!isalpha(buffer[i])) {
                 buffer[i] = 0;  
                 break;
             }
     }
     

        if (!foundWord(list, buffer,count))  /* Compiler error here about change of form.  I added the count and the error cleared but I think the logic is wrong*/
            strcpy (list[count++], buffer);      
    }
    return count; ?* declaration syntax  error*/
} /*declaration sysntax error/


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

     int i;

     for(i=0;i<size;i++)
         if (stricmp(list[i],word)==0) return1: /* compiler doesn't recognize this code (I thought it meant compare without regard to letter case*/
         return 0;
}
0
 
efnCommented:
> If you look at my code does it seem sensible.
   
No, it looks garbled, possibly in the posting process.  For example:

> nt insertWord(char list[][MAat I did here.*/*error here about buffer not being intialized before use.  I thought that was what I did here.*/

--efn
0
 
jholmes9186Author Commented:
I don't understand this response.  
0
 
jholmes9186Author Commented:
Sorry,  I see what you mean.  Here it is again.

#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][30];

     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);
       printf ("There are %d unique words in the file pgm6.txt",wordcount);
     return 0;
}


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) return1:
         return 0;
}



0
 
efnCommented:
OK, that's better.  A few hints:

The for loop in insertWord is operating on garbage, because the code never put anything in the word array.

The count variable in insertWord is used, but never set to anything.

The foundWord function needs a space between "return" and "1".  There should be a semicolon (';') instead of the colon (':') after the 1.

stricmp is not in the standard library.  You may need to include another header file to get it declared.

The logic in insertWord is faulty.  The while loop fills up list (and possibly beyond) with strings from the input file, then the rest of the function operates on the garbage in word.  You should have a loop that terminates either on the end of the input file or when the array is full.  The loop body should read one word, look it up in the array, and add it if it's not already there.  The loop body you have now is just "i++;".  To put more stuff in the loop body, use a block enclosed in braces ('{', '}').

--efn
0
 
jholmes9186Author Commented:
Thanks for the response.  As a relative newcomer to C (and programming) I do not understand the problem with the logic.  Could you provide a little more clarification.  This assignment was from my girlfriends intro to C classes and looked interesting so I am giving it a run but seem to be losing the battle.  Any more specific help you can provide would be greatly appreiciate.
0
 
efnCommented:
In a while loop, you first specify the terminating condition, and then the loop body, the stuff that is to be run over and over while the condition is true.

So in this loop:

 while (fscanf(fp,"%s",list[i])!=EOF)i++ ;

It tries to read a string into list[i].  If fscanf doesn't return EOF, it increments i and loops.  So all this is going to do is read strings into list.  It's not going to do anything with every word it reads other than store it in list.

One problem with this is that if the input file has more than 1,000 words, the code will happily write past the end of the array.  This is undesirable.  It tends to break your program in such a way that it's hard to find the problem.  Neither the compiler nor the run-time system will slap your hand and say "You wrote beyond the bounds of your array, clown."  They will just let the program do it and corrupt some other variable.  If this is just an educational program and you know the input will never have more than 1,000 words, you don't have to worry about this, but it would be required for an industrial-quality product.

If you want the list to contain only unique words, you need to pick an algorithm to accomplish this.  You could read all the words in first, and then remove duplicates, or you could look up each word as it is read and store it only if it is not already in the list.  Some pseudocode for the second approach:

while the array is not full and we have not hit the end of the input file
{
  try to read a word
  if that worked
  {
    look up the word in the array
    if it's not already there
      add it
  }
}

Hope this helps...

--efn
0
 
jholmes9186Author Commented:
I tried to accept the answer and it said permission denied! What does this mean?
0
 
sunnycoderCommented:
you already have accepted an answer :)
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!

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