Hi everyone,

this is what I have to do:

The method

makeWordList (char *fileName)

. this method will take in a file name, read from the file and identify the words in the file. These words should be stored in a global array for use in the next method.
The method

char **getWordList()

should return the list of words. Since each word is of type char* an array of such words is of type char**, which is what the method should return.
The method

getNumWords()

should return the number of words found. The makeWordList() method should store this someplace convenient for getNumWords() to return.

This is what I have so far:

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


int const max_words = 30;
int const max_char = 20;

int wordcount=0;
int charcount=0;
char wordlist[max_char][max_words];

void makeWordList(char*fileName) 
{
	FILE *inputFile;
	
	int spacecount =0;
	char ch;

	inputFile = fopen(fileName, "r");
	if ((inputFile = fopen(fileName, "r"))==NULL)
		printf( "file cannot open.\n");
	else
	ch=fgetc(inputFile);
	  while (ch!=EOF){
		  while (charcount<=max_char && wordcount<=max_words){
			if ((ch>=48 && ch<=57)||(ch>=65 && ch<=90)||(ch>=97 && ch<=122)){
				wordlist[charcount][wordcount]=ch;
				spacecount=0;
				charcount+=1;
			}
			else if (spacecount==0){
				wordlist[charcount][wordcount]='\0';
				wordcount+=1;
				charcount=0;
			}
			spacecount+=1;
		  }
	  }  
		  
	  fclose(inputFile);
  }
char **getWordList()
{
	return wordlist; /*I am not sure how to do this one*/
}
int getNumWords(){
	return wordcount;
}
void findReversals (char *fileName1, char* fileName2)
{
	char **words1=0;  
	char **words2=0;
	int i, numWords1, numWords2, j, k;
	
	makeWordList (fileName1);
	words1 = (char**) getWordList ();
	numWords1 = getNumWords ();

	makeWordList (fileName2);
    words2 = (char**) getWordList ();
	numWords2 = getNumWords ();

	char *str1=NULL;     // String initialization.
	char *str2=NULL;
	char ch1=NULL;
	char ch2=NULL;
	int len1 =0;
	int len2=0;
	int flag;
	int rev =0;
 
	for (i=0 ; i<numWords1 ; i++)
	{  
		flag =0;
		len1=strlen(words1[i]);
		str1= words1[i];
		rev=len1;

		for (j=0 ; j<numWords2; j++)
		{
			len2=strlen(words2[j]);
			str2=  words2[j];      
			if (len1 == len2)
		{ 
       
		for (k=0; k<len1; k++)
		{
			ch1= str1[k];
			ch2= str2[rev-1];
     
		if ( ch1 != ch2)
		{
			flag = 0;       
		}      
		else
       {
             flag =1;
       }
   
       rev--;
        
      } // end of for loop
   
    }// end of if
    if (flag == 1)
       {
       printf("yes this is a reversal");
 
       } 
    else
      {
       printf("No this is not a reversal");
 
       }
   
  } // end of for
}

}

void printWordList (char *fileName)
{
  char **words;
  int i, numWords;

  makeWordList (fileName);
  words = (char**) getWordList ();
  numWords = getNumWords ();
  printf ("Words in file %s\n", fileName);
  for (i=0; i<numWords; i++) {
    printf ("  %s\n", words[i]);
  }
}

 int main (int argc, char **argv) 
{
  printWordList ("ex2test1.txt");
  printWordList ("ex2test2.txt");
  printWordList ("ex2test3.txt");
  findReversals ("ex2test2.txt", "ex2test3.txt");
}

How can I return the list using the method getWordList?

Recommended Answers

All 5 Replies

Sure, but that would take up more memory.
You would need to store an array of char* somewhere.

Please, explain it to me.

char *words[n];
words[0] = wordlist[0];

And so on.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.