Hello,

I have a question
I should write a program that read words from file text and sort it by ABC letters in array, and ask the user to enter a sentence and chcking if the words in the sentence existing in the array.
the program checking all the words in the sentence, If it found a word that didn't exist in the array,it show to the user a message that tell him that the "word" not exist and show for him an altenatives words to replace it, and ask the user if he want to replace the word, if yes, ask him to which word he want to replace.
and the program will change the words in the sentence.

I write the all, and all the code is correct, but when i call the func that replace the words I have a problem in the second parameter in the func

plz if anyone can help me.

this is the code

#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 CheckWord (char ch[]);
void replace(char word[],char search[],char repl[]);


void main()
{
    char dic[W][L];
    char word [M];
    char *split;
    int i=0;
    int m=0;
	int choice;
	char answer;
    sortarray(dic);
    printf("enter sentence : ");
 
 
    gets(word);
    split=strtok(word," ");
 
    while(split!=NULL)
    {
		if (!CheckWord(split))
		{
			printf("%s is not a word in our vocabulary, you may want to switch this word to the following substitutes:\n",split);
			for(i=0;i<W;i++)
			{
				if(strncmp(split,dic[i],3)==0 && m++!=5)
					printf("%d. %s\n",m,dic[i]);
			}
			m=0;
			printf("Would you like to switch this word?\n");
			scanf("%c", &answer);
			if(answer=='N' || answer=='n')
				return;
			else
				if (answer=='Y' || answer=='y')
				{
					printf("To which substitute?\n");
					scanf("%d", &choice);
					replace(word,split,dic[choice]);
					puts(word);
				}
									
		}

		split=strtok(NULL," ");

	}

	getchar();

}
 
 
 

//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);
			}
}


void replace(char word[],char search[],char repl[])
 {
	int len = strlen(word);
	int s_len = strlen(search);
	int r_len = strlen(repl);
	char *current = word;
	while (current = strstr(current, search))
	{
		memmove(current + r_len , current + s_len,  len - (current - word) - s_len + 1);
		strncpy(current, repl, r_len);
	}
}

>>and all the code is correct
Then why is getWord() never called to read the words from the file?

You are on very dangerous ground with that replace function. The intent of the function is to replace one word with another word within the sentence. Lets say I enter a sentence that has 49 character, the max allowed in the word array. Now lets assume I want to replace the word "Iowa" with the word "Mississippi", which is longer than the original word. Result: buffer overflow because word can't contain all those extra characters.

>>I have a problem in the second parameter in the func
What is the problem with it?

hello,
I called the getword() func in the checkValid() Func.
so, when I called the CheckValid Func I called too the getword() func.

I didn't know what the problem in the second parameter.
but try to call the replace() func by this code:

replace(word,"favorcy","favor");

you will see that the func replaced the words

"When arrays are declared as function parameters then they are treated as pointers and not arrays"....I think this would help you out...

This article has been dead for over six months. Start a new discussion instead.