Solved

READING AND COMPARING STRINGS

Posted on 2004-08-02
12
678 Views
Last Modified: 2010-04-15
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
Comment
Question by:jholmes9186
  • 6
  • 3
  • 3
12 Comments
 
LVL 45

Expert Comment

by:sunnycoder
Comment Utility
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
 
LVL 45

Expert Comment

by:sunnycoder
Comment Utility
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
 

Author Comment

by:jholmes9186
Comment Utility
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
 

Author Comment

by:jholmes9186
Comment Utility
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
 
LVL 15

Expert Comment

by:efn
Comment Utility
> 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
 

Author Comment

by:jholmes9186
Comment Utility
I don't understand this response.  
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Author Comment

by:jholmes9186
Comment Utility
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
 
LVL 15

Expert Comment

by:efn
Comment Utility
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
 

Author Comment

by:jholmes9186
Comment Utility
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
 
LVL 15

Accepted Solution

by:
efn earned 200 total points
Comment Utility
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
 

Author Comment

by:jholmes9186
Comment Utility
I tried to accept the answer and it said permission denied! What does this mean?
0
 
LVL 45

Expert Comment

by:sunnycoder
Comment Utility
you already have accepted an answer :)
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now