i need a program , that receives an input file with words
and a puzzle and gives an output file, with the words and their coordenates in the puzzle.


input has the number of words (3 in this case), the words to be found,
and the text where the words live (puzzle), the output has the words the user wants to be found and their coordenates.
the words lenght is max 15 and the puzzle max is 30

So The input file and the output look like:

|input.txt.............|---->|output.txt |
|______________|---->|___________|
|3.......................|---->|...................|
|word1................|---->|word1 (6,1) |
|word2................| --->|word2 (5,2) |
|word3................|---->|word3 (1,3) |
|.........................|---->|...................|
|.........................|---->|...................|
|abcdeword1fg.....|---->|. |
|hijkword2lmn......|---->|. |
|word3opqrstu......|---->|. |
|_______________|---->|___________|


I am considering the words the user wants to find as "cadena"
(word1, word2...wordn) and the text where the word is supossed to be "cadena2" (bidimentional array)


My idea is to scan the 'cadena' until it is null, (word1,word2,...wordn) given in the input.txt and each one is compared with 'cadena2'
(the puzzle);

If the program finds the word, in the puzzle, it returns in the output file, the word 'cadena' and its coordenate other it returns "Not found".


I have done this, please help me with this program;

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

FILE *ArchivoEntrada;      ////INPUT FILE//
FILE *ArchivoSalida;       ////OUTPUT FILE//

void main()
 {
  char cadena[15];         ///WORD WANTED MAX LENGHT//
 char cadena2[30];       //PUZZLE LINE MAX///
 int x,y;

 ArchivoEntrada=fopen("E:\entrada.txt", "r");
 ArchivoSalida=fopen("E:\salida.txt", "w");
    if (ArchivoEntrada==NULL || ArchivoSalida==NULL)
       {
        printf("ERROR");
        getch();
        return;
       }
   else
             while(!feof(ArchivoEntrada))
                  {
                   
                   fscanf(ArchivoEntrada,"%s%d%d", &cadena,&x,&y);  //??//
                  
                   while(cadena!=\'0')
                      {
                      if(!strcmp(cadena2, cadena))
                         {
                     fprint("%s: (%d) %d \n", cadena, x, y); 


 
             //how to wite the words with their coordenates coordenates?? ///
            //i got a very useful help, before in that topic, buy how could//
          ///solve this problem//
 


                          break;
                         }
                      else
                          fprintf("\nNot Found") 
                          break;
                           
                      }
                  }                          
                  clrscr();
                  printf("\nSUCCESS!! \n");
                  getch();
                  fclose(ArchivoEntrada);
                  fclose(ArchivoSalida);
       }



                ///this is other code i have//////


             search(char *cadena[], char *nombre)
               {
                 register int i;

                   for(i=0; p[i]; i++)
                     cadena=strrchr(cadena2,'cadena');            /////See the last occurrence os cadena1// 
                     if(!strcmp(p[i], nombre, p-cadena+1)) //////
                      return i;
                     else
                     return -1;
               }

IS MY IDEA RIGHT, CAN I COMPARE THE words given with the puzzle like i said, or am i wrong??
HOW cAN I SEARCH A WORD IN AN INPUT.TXT, AND RETURN THE SAME WORDS WITH THEIR COORDENATES IN ANOTHER FILE OUTPUT.TXT????:rolleyes:

Not on topic:
1) it should be int main(), not void main().
2) don't use the return result of eof() as the conditional for a loop to run or not. Sooner or later it will get you into trouble.

On topic:
1) You want to read from file to cadena2, not cadena
2) You need to have something in both cadena2 and cadena when sending them to strcmp()
3) Using strcmp() just looks for equivalence of the two strings. I suspect you want to see if cadena is a substring of cadena2, so strstr() or something similar may be more pertitent.

4) Using this approach you are limited to the desired string being found in the target string and both going in the same direction. If you want to see if the desired string is present, but backward in the target string, then this approach will not work. Likewise, many wordfinding puzzles require you to look for desired words in grid of char, not a string of char, and the desired words may be oriented horizontally, vertically, or at an angle and may be going bidirectional in each orientation. Your code has no chance of doing anything like that.

5) I see no way to get an x and a y coordinate indicating where in a grid the desired word is ound in your code.


If you want/need to be able to look for desired words in all orientations bidirectionally, or some subgroup beyond horizontal and left to right, then I would declare cardena2 to be a two dimensional array of char. I would fill cardena2 from information in file 1 char at a time (although in theory you could do it one row or line at a time, depending how the file contents correlate with a two dimensional array). I would also declare cardena to be an array of strings, which could also be a 2 twomensional array of strings if desired. I would then use a nested loop to find the location of the first letter of each string/word in cardena in cardena2 and report the row/column (x/y) of the first letter of cardena in cardena2.

Well I don't think I'm doing so muxh progress
but is it right to use 2 structs in a program???,

struct cadena2{                     //the puzzle
          char cadena2[30][30];
         };


struct cadena{                      //the word
          char cadena[15];
          int   coordinates;
          };

But how could i use pointers, or how to get the coordinates in that array???
:sad:

How do you fill the matrix with the data of the file, how do you compare the puzzle with the words given in the file??? (like to give something to a function to compare)

And how to modify that thing of eof??

Evaluate the return value of the input function rather than the return value of eof() to regulate the loop. fscanf() returns the number of character read in or EOF if it fails and fgets() returns NULL if it fails. Either function could be used to read in the file contents line by line. While fgets() may be the prefered method, given the constraints of your assignment, fscanf() should work as well.

Your posts seem to indicate that you only need to determine if the desired string is a substring of the input string, and if so, what line is it on (the x component) and where does it start within the string (the y component).

I don't think you need to use structs to solve that problem, but you could if you wanted to.

To hold the words to find I'd use an array like this;
char cadena[numberOfWords][maxLengthOfAWord + 1];

If you search each line of file as it's read in for each word in cadena then you could use this:
char cadena2[maxLengthOfLine + 1];

In this case you keep track of which line you're on as read the lines in.

I you read the entire file in one line at a time and then search the container for the desired words I'd declare cadena2 like this:
char cadena2[maxLines][maxLengthOfLine + 1];

strstr() will tell you if a given word in cadena is in cadena2 or cadena2, depending on which protocol you use. strstr() returns a pointer to the start of the substring in the full string (the line you're searching would be the x component), but it won't tell you the position of the substring in the full string (the y component), so you'll need to scan the line to find where the word starts.

Hi, well,
thanks for the help, but i have one more question

if the program receives and input.txt

in the way

|input.txt ____|
|3...................|
|word1............| <---one pointer
|word2............|
|word3............|
|.....................|
|.....................|
|abcdeword1fg.| <----other pointer
|hijkword2lmn..|
|word3opqrstu..|
|____________|

I´m having lots of trouble putting a pointer in the word1, and then other pointer in the puzzle, how can i tell the program to recognize a part of the file, and put a pointer, and then recognize other part of the file and put another pointer?????

i think i have done a funtion that finds the first letter of the word1 in the puzzle and then continues with the other next to it, other moves on til it find it.

But i dont know how to use the pointer,
how to do the funtion to word1, and when its done, how to continue to word2, and then word3, until there are no more words to find in the puzzle.

Please help me,
Could you put the code for the pointers
that are being read from input.txt
I have no idea

The first line of the file is apparently the number of words to find, in the case just posted, 3. Since the 3 is there I assume the file may have something other that 3 and you won't know until you read the program. However, you can read the first line in with fscanf() and use that result to dynamicallly declare cardena since you've already been told that the max word length is 15 char.

Then use a loop to read the next 3, 4, 2, 10 or whatever lines there are and store each word read in in cardena.

Once you've read all the words to find into cardena then you could read in rest of file 1 line at a time into cardena2 and search each value of cardena2 for each word in cardena, keeping track of which line of cardena2 you are on, which word in cardena you found, and where in cardena2 you found the first letter the word in cardena2. If you found a word in cardena in the current cardena2 then you could write the word in cardena you found, which line of cardena2 you found it on, and where in cardena2 you found the first letter to the output file before getting the next line for cardena2 from the file.

If there can be blank lines in the file, then you will need to be able to handle them as well, though, depending on what input function you used it may be a mute point, as I think fscanf() ignores leading whitespace, though I won't gaurantee that (or anything else I post either, for that matter).

Read the puzzle into a 2-dimensional array. Then you can easily look at each character in the array and you know the coordinates (upper left corner would be puzzle[0][0] Then look at the first letter of the work you are searching for.
1) Find that letter in the puzzle.
2) When you find one, check all the locations next to it for the second letter. 3) When you find one, check the third letter with the next letter in the puzzle, in that same direction (found the second letter above first, check the next letter above that one).
4) Repeat until you've checked all the letters in the word or a mismatch
5) If you didn't find the solution, continue checking the rest of the second letters in step 2.

In theory, WaltPs approach could work too; the only problem is the file apparently doesn't indicate how many lines the puzzle will have, only that each line/row will be no longer than 30 char and each word to find will be no longer than 15 char each. Or at least that's how I interpret the information in the first post. If I knew the puzzle was a 15 by 30 char array, or that both the number of rows and the size of the rows were available somewhere, then I'd feel much more comfortable with WaltPs protocol. WaltPs char by char evaluation would allow the game to be extended to finding words aligned horizontal, vertical, or diagonal, and in any direction right to left, left to right, top to bottom, or bottom to top as I talked about in my first post.

Hi there, well I still don't get it,
how to declare a pointer and then use it, HOW TO RECOGNIZE A SPECIFIC DATA IN THE FILE, AND THEN USE IT,
and the (!feof), :eek: I DONT UNDERSTAND
This is in what i have been working, but i dont get any result
please help
:sad:

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<stdio.h>

FILE *ArchivoEntrada;
FILE *ArchivoSalida;
int buscaensopa(char *listanpalabra, char npalabra, char sopa);

void main()
   {
    clrscr();
char npalabra[15];     //the words that are on the list, the number 'n'// 
int length;           
int fila;                     //row//
int columna;             //column//
char listanpalabra[15];  //the list itself, it conteins the words to scaned/
char sopa[30][30];           //the puzzle//
 
 
//open file and //
   ArchivoEntrada=fopen("E:\\entrada.txt", "r");
   ArchivoSalida=fopen("E:\\salida.txt", "w");
      if (ArchivoEntrada==NULL || ArchivoSalida==NULL)
     {
      printf("ERROR");
      getch();
      return;
     }
      else
 {
  while (!feof(ArchivoEntrada))       ///HOW TO CHANGE THIS//
   {
 
////HERE IS THE PROBLEM; HOW TO POINT TO SPECIFIC PLACES OF THE ENTRADA.TXT?? "THE LISTANPALABRA[15], SOPA[30][30]?? AND THEN USE THEM ////////////////////////////////

  fscanf(ArchivoEntrada,"%s", &listanpalabra[15], &npalabra[15], &sopa[30][30]);
 

  fprintf(buscaensopa(char *listanpalabra, char npalabra, char sopa));
/////////////////////////////////////////////    
 
}
     fclose(ArchivoEntrada);
     fclose(ArchivoSalida);
}
 
 
 
int buscaensopa();
{
int nfila,ncolumna,npalabra;
char direction[15]={"Izquierda","Derecha"};
int i,j,k,n;
    for(i=0;i<npalabra;i++)
    {
     found=0;
     {
      for(j=0;j<fila;j++)
       {
 for(k=0;k<columna;k++)
 {
  if(sopa[j][k]==listanpalabra[i].npalabra[0])
  {

   //izquierda//
   if(listanpalabra[i].length<=(k+1))
   {
    for(n=0;n<listanpalabra[i].length;n++)
    if(listanpalabra[i].npalabra[n]!=sopa[j][k-n])
    break;
    if(n==listanpalbra[i].length)
      {
       found=1;
       listanpalabra[i].fila=j;
       listanpalabra[i].columna=k;
       listanpalabra[i].direccion=Izquierda;
       }
    }
     //derecha//
     if(listanpalabra[i].length<=(mcolumna-k))
       {
       for(n=0;n<listanpalabra[i].length;n++)
       if(listanpalbra[i].npalabra[n]!=sopa[j][k+n])
       break;
       if(n==listanpalabra[i].length)
  {
   found=1;
   listanpalabra[i].fila=j;
   listanpalabra[i].columna=k;
   listanpalabra[i].direccion=Derecha;
  }
       }
  }
 if(found==1)break;
       }
 if(found==1)break;
     }
    }
 for(i=0;i<npalabra;i++)
    {
     if(listanpalabra[i].fila!=-1)
        {
fprintf("\n //palabra en (x,y)//",listanpalabra[i].npalabra,listanpalabra[i].fila+1),listanpalabra[i].columna+1);
fprintf("\n//Direccion://"direccion[listapalabra[i].d]);
cout<<"\nlength "listanpalabra[i].length);
        }
}
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.