hello,

I have a matrix (string array that have words) and I want to write a function that receive the word and consider whether the word existed or not in the matrix
I"m trying to write the function, but i didn't succeeded.

this is my code

int CheckWord (char ch[L])
{
	int i=0,j=0,k=0;
	char dic[W][L];
	for (i;i<L &&j<L; )
	{
	while (ch[i]!=dic[k][i])
	{
		k++;
	}
	return 1;
	}
	
return 0;
}

What errors do you get? You only posted code snippet so its not possible for us to compile/test your code.

you declared dic but never filled it with anything. So how in the world do you expect that loop to find a word in the dictionary?

Why don't you just use standard C function strcmp() instead of that loop? (maybe your instructor won't let you??)

You need a different loop to loop through each of the strings in dic

for(k = 0; k < W; k++)
{
     if( strcmp(dic[k], ch) == 0)
     {
           // found it, so now do something here
    }
}

tahnk you :)
I know where is the problem and solute it
I was should use another func that I wrote it before

thanks again :)

this is the code :

int CheckWord (char ch[])
 {
	int i;
	char dic[W][L];
	sortarray(dic);
	for(i=0;i<W;i++)
		if(strcmp(ch,dic[i])==0)
			return 1;            
    return 0;  
 }

Now of course that function has the problem is populating the dictionary array. Where is it getting the strings from? I would suggest that dic array be declared and populated in main() then passed to CheckWord() as another parameter so that it doesn't have to be populated each time CheckWord() is called.

I don't understand what you mean exactly, but the function helps from anther func sortarray(), it's called it and compare from the word that I send it and the array in the sortarray()

The array dic does not contain any words, it is nothing more or less than an uninitialized two-dimensional array -- that doesn't contain anything. Before you can use that array you have to put some words into it. Where do those words come from? My guess would be a file that contains a bunch of dictionary words.

yes of course, there is a file that contain words (dic) that i compare the word with it

If there is a file then why isn't that program reading it into the array?

I read it in another func
I have some functions
one that read the file
another that sort the words on the file by ABC letters
and another that compare between the word that the user enter it and the dic array

Then apparently the dic array is not declared in that function like you posted? In that case, just ignore me :)

I will write the all code
you will understand the program and may you can help me
I want to let for the user to enter a sentence.
And I want to take the words from the sentence and compare each word with the dic, if the word doesn't match on the dic I will order from the user to change it by another words that found on the dic, this words I will show it to the user (I will show maximum 5 words) in condition that the word that i will show it to the user should be the first letters are similars.
this is the code

and I will give you running example for the program in the next reply

#include <stdio.h>
#include <string.h>
#define INPUT "stockWord.txt"
#define L 12 // the length of each word
#define W 50 //the maximum words on the dictionary
#define M 100 // the maximum letters on the sentence that the user will enter it.


int getWord(char dic[][L]);
void sortarray(char dic[][L]);
int PgetWord(char dic[][L]);
void Psortarray(char dic[][L]);
int CheckWord (char ch[]);


void main()
{
    char dic[W][L]; //array for the dictionary
	char word[L]; //array for the word that the user enter it
	char sen[M]; //array for the sentence that the user will enter it.
	int i;
	char c;
	sortarray(dic);
	printf("enter sentence:\n");
	gets(sen);

}
 

//inputing the words from the text file and outputing the array.
int getWord(char dic[][L])
{
	FILE* file;
	int i,j;
	char ch;
    file=fopen(INPUT,"r");
 
    for(i=0;i<W;i++)
    for(j=0;j<L;j++)
    dic[i][j]='\0';
	i=0;j=0;
   do
   {
    ch=fgetc(file);
    if(ch!='\n')
    {
    dic[i][j]=ch;j++;
    }
    else if(i<W)
    {j=0;i++;}
 
   }
   while(ch!=EOF);
   
 
	return 1;
 
 
	fclose(file);
 
}


int CheckWord (char ch[])
 {
	int i;
	char dic[W][L];
	sortarray(dic);
	for(i=0;i<W;i++)
		if(strcmp(ch,dic[i])==0)
			return 1;            
    return 0;  
 }
 



//sorting the array by the ABC letters.
void sortarray(char dic[][L])
{
	int i,j;
	char tmp[L];
	getWord(dic);
	for(i=0;i<W-1;i++)
		for(j=0;j<W;j++)
			if(strcmp(dic[i],dic[j])<0)
			{
				strcpy(tmp,dic[i]);
				strcpy(dic[i],dic[j]);
				strcpy(dic[j],tmp);
			}
}





//to print the array
int PgetWord(char dic[][L])
{
	FILE* file;
	int i,j;
	char ch;
    file=fopen(INPUT,"r");
 
    for(i=0;i<W;i++)
    for(j=0;j<L;j++)
    dic[i][j]='\0';
	i=0;j=0;
   do
   {
    ch=fgetc(file);
    if(ch!='\n')
    {
    dic[i][j]=ch;j++;
    }
    else if(i<W)
    {j=0;i++;}
 
   }
   while(ch!=EOF);
   for(i=0;i<W;i++)
   {
	   for(j=0;j<L;j++)
	   {
		   if(dic[i][j]=='\0') 
			   break;
		   printf("%c",dic[i][j]);
	   }
	   printf("\n");
	   if(dic[i][0]=='\0') break;
   }
 
	return 1;
 
 
	fclose(file);
 
}



// print the array after sorting
void Psortarray(char dic[][L])
{
	int i,j;
	char tmp[L];
	getWord(dic);
	for(i=0;i<W-1;i++)
		for(j=0;j<W;j++)
			if(strcmp(dic[i],dic[j])<0)
			{
				strcpy(tmp,dic[i]);
				strcpy(dic[i],dic[j]);
				strcpy(dic[j],tmp);
			}
	
	for(i=0;i<W;i++)
    {
	if(dic[i][0]!='\0')
		printf("%s\n",dic[i]);
	}
}

Just as I suspected -- the array dic in CheckWord() is NOT the same as declared in main(). Delete the dic array in CheckWord() and pass the array as a parameter like you did with other functions.

getWord() -- reverse the last two lines so that the file is closed before returning from the function.

example for sorted array:

again
and
extend
extension
extern
extinction
father
fatling
fatly
fatness
favorable
favorably
favored
favorer
favorite
favoritism
for
good
late
later
lest
more
making
maybe
something
to
tomorrow
yes

example for sentence that the user is entered:
again and favorcy making less extention

for each word on the sentence that doesn't match in the sorted array will shows message tell the user that the word not exist, for example:
favorcy is not a word in our vocabulary, you may want to switch this word to the following substitutes:
1. favorable
2. favorably
3. favored
4. favorer
5. favorite

Would you like to switch this word? Y
To which substitute? 2

extention is not a word in our vocabulary, you may want to switch this word to the following
substitutes:
1. extend
2. extension
Would you like to switch this word? Y
To which substitute? 5
No such substitute, choose again!
To which substitute? 2
less is not a word in our vocabulary, you may want to switch this word to the following substitutes:
1. lest
Would you like to switch this word? N
Your sentence after checking is:
again and favorably making less extention
Would you like to enter another sentence? N

sorry, but I didn't understand the problem !!!
the program right now workes well !!!

Apparently your program is never calling CheckWord() because if it did that function would always fail to find the word for the reasons I have already mentioned twice and will not repeat any more.

so, What I should do to calling CheckWord() always?

write the rest of the program and you will find where you need to call that function. The last thing you did in main() was to get keyboard input for the sentence. How you have to run that sentence through the CheckWord() function.

main() returns an int. Why? Click here
gets() is not a function that you should use. Why? Click here.
Some improvement in proper indentation would make it easier to read. Click here.

the problem that I doesn't know what I should to

I write this code in the main:

printf("enter sentence:\n");
	gets(sen);

	while (gets(sen))
	{
		scanf("%s", &sen);
		printf("%d",CheckWord(word));
	}

but it doesn't worked !! :(

What I should do ?

>What I should do ?
Practice a little bit more how to obtain input from stdin.
You are not understanding how it works, right now.

In order to receive input from keyboard you need to have space allocated for it, right? Thus:

char sen[M] /* which can store up to 99 char + '\0' */

gets() doesn't allow you to put a limit to how much it should read from stdin, therefore, it can very well overflow the maximum space that sen[] has.

fgets() could be an alternative. And in this case can be used as this:
fgets(sen, M, stdin);

sen is the place where you store the data.
M is the maximum space you want to set apart to store the data, minus one for the string terminator '\0'.
stdin is from where you are going to receive the data.

fgets() stops reading when you tell it by via of M, when it sees a '\n' (newline), or when there's no more data; whichever occurs first.
If it can not be successful reading it will return a NULL pointer.
That means you could always check the result of fgets() like:

if ( fgets(sen, M, stdin) ) {
    /* do something */
}

or...

while (fgets(sen, M, stdin)) {
   /* a line was read do something with it */
 } /* I will loop and try to read another line */
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.