Hi, well i take lots of time figuring this out.

I need a program that tells me the coordinates of a word that is in other array,

what I have gotten is the y coordinate "the column", but
I need to do the case for a bidimentional array
get the x coordinate too "the row".

so this look like

hard //word to be found//

blablablablablabalblablabalbla //the puzzle where the word is
imtriyingtodomybestbutishard
blablablablablbalablabalabalbl


so the result i get from this is : (2,25) //row, column//
IN THE EXAMPLE I ONLY GET THE 25, AND I NEED TO GET THE 2 TOO

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


main()
{
 int coordenada(char *string, char *target);

 printf("%d\n", coordenada("mellevalachingadaporqueestoestamalplan", "mal"));
 getch();
 return 0;

}
 //returns the position of t in s//

 int coordenada(char *s,char *t)
 {
  char *p;

  for(p = s; *p != '\0';p++)    //p runs trough s //
      {
       char *x, *y;             //the first letter 'y' = 't'//

       x=p;                   //now keep going til find the end of the word//
       y=t;

    for(; *x != '\0' && *y != '\0' && *x == *y; x++, y++);

       if(*y == '\0')       //we get to the end of the word

          return p - s + 1;    //this gives the coordinate 'y'//


      }
      return -1;
             //if it fails//
 }

PLEASE HELP ME , I NEED TO FINISH THIS BY THE END OF THIS DAY
PLEASE!!:-|

You're not even using a two dimensional array. But if you were, did it occur to you that you can do this?

for ( i = 0; i < ROWS; i++ ) {
  int col =  coordenada ( array2d[i], target );

  if ( col != -1 )
    printf ( "\"%s\" found at (%d,%d)\n", target, i, col );
}

thanks, sorry for so many questions, but im newbie


how do i define the array2d

how could i use the code you have in my code??? does it go in first place?? dont know how to put all in place.

like:

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


main()
{
 int coordenada(char *string, char *target);

 printf("%d\n", coordenada("mellevalachingadaporqueestoestamalplan", "mal"));
 getch();
 return 0;

}
 //returns the position of t in s//

 int coordenada(char *s,char *t)
 {
  char *p;
   
  //////?????????????///
  int i;
  char ROWS;
  char col;
  char array2d[3][10]
for ( i = 0; i < ROWS; i++ ) {
  int col =  coordenada ( array2d[i], target );

  if ( col != -1 )
    printf ( "\"%s\" found at (%d,%d)\n", target, i, col );
}

/////////////'?//

  for(p = s; *p != '\0';p++)    //p runs trough s //
      {
       char *x, *y;             //the first letter 'y' = 't'//

       x=p;                   //now keep going til find the end of the word//
       y=t;

    for(; *x != '\0' && *y != '\0' && *x == *y; x++, y++);

       if(*y == '\0')       //we get to the end of the word

          return p - s + 1;    //this gives the coordinate 'y'//


      }
      return -1;
             //if it fails//
 }
Member Avatar for iamthwee

If you are only just realising you have to use a 2d array now, you've no hope of getting this done by today.

:lol:

well i dont know exactly how to define it well

>but im newbie
I don't care. You ask questions, I answer them. If I need to know your level of experience, I'll ask a pointed question to determine that. Calling yourself a newbie serves no purpose at all.

>how could i use the code you have in my code???
I was thinking that you define the array and just paste the code I gave you into main as well:

#include <stdio.h>

#define ROWS 3

int coordenada ( const char *s, const char *t )
{
  const char *p;

  for ( p = s; *p != '\0'; p++ ) {
    const char *x = p, *y = t;

    for ( ; *x != '\0' && *y != '\0' && *x == *y; x++, y++ )
      ;

    if(*y == '\0')
      return p - s + 1;
  }

  return -1;
}

int main ( void )
{
  const char *array2d[] = {
    "blablablablablabalblablabalbla",
    "imtriyingtodomybestbutishard",
    "blablahardlablbalablabalabalbl"
  };
  const char *target = "hard";
  int i;

  for ( i = 0; i < ROWS; i++ ) {
    int col =  coordenada ( array2d[i], target );

    if ( col != -1 )
      printf ( "\"%s\" found at (%d,%d)\n", target, i, col );
  }

  return 0;
}

It's not exactly brain surgery if you have a book on C.

thank you very much

Hello, huehuehe took some time to solve this, like when you know how to program is easier, so, thanks to all.

here es the code.

Hola, me puse a resolver esto, ya que cuando iniciaba, no tenía ni idea de como hacerlo, ahora me contesto a mi mismo.
este es el codigo:

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
#include <ctype.h>
#define MAXLINEA 180 //max tamaño de una linea

//busca una palabra en una linea, si esta, regresa la coordenada 'x'
int coordenada ( const char *linea, const char *palabra ){
    const char *p, *x,*y;
    for ( p = linea; *p != '\0'; p++ ) {
        x = p;
        y = palabra;
        for ( ; *x != '\0' && *y != '\0' && *x == *y; x++, y++ )
            ;
            if(*y == '\0')
                  return p - linea + 1;
    }
return -1;
}

//reservamos espacio para nuestro arreglo 'bidimensional'
char **reservaEspacio(int filas, int cols){
   char **arreglo;
   int i;
   /* espacio para el arreglo de apuntadores a entero*/
   arreglo = (char**)malloc(filas * sizeof(char*));
   if(arreglo == NULL){
      fprintf(stderr, "Error 1-d \n");
      exit(-1);
   }                                                                               
   /* espacio para los arreglos de letras */
   for(i = 0; i < filas; i++){
      arreglo[i] = (char*)malloc(cols * sizeof(char));
      if(arreglo[i] == NULL){
         fprintf(stderr, "Error 2-d\n");
         exit(-1);
      }
   }                                                                               
   return arreglo;
}

//regresa un arreglo 'bidimensional', llenandolo a partir
//de un archivo de entrada 'entrada.txt'
char **lee (int *numPalabrasPtr, char *nombreArchivo) {
  char linea[MAXLINEA];   
  char **arreglo;
  int i;
  FILE *f;
  if ( ! ( f = fopen (nombreArchivo, "r") ) ) {
    printf ("error al abrir archivo %s\n", nombreArchivo);
    exit (-1);
  }
   while ( fgets (linea, MAXLINEA, f) ) {
         *numPalabrasPtr = *numPalabrasPtr+1;
  }
  arreglo = reservaEspacio (*numPalabrasPtr, MAXLINEA);
  rewind (f);
  for ( i=0 ; i <*numPalabrasPtr ; i++ ) {
    fgets (arreglo[i], MAXLINEA, f);
    arreglo[i][strlen(arreglo[i])] = '\0';
  }
  fclose (f);
  return (arreglo);
}

//busca la palabara en el arreglo, si esta,la imprime
// y escribe en un archivo 'salida.txt'
void busca(int renglones, char **array2d , char *target){
     FILE *sal;
     sal = fopen("salida.txt","a+");     
     int i, col;
     for ( i = 0; i < renglones; i++ ) {
         col =  coordenada ( array2d[i], target );
         if( col != -1 ){
             fprintf(sal,"\"%s\" \t encontrada en (%d,%d)\n",target, i+1, col);             
             printf ( "\"%s\" \t encontrada en (%d,%d)\n", target, i+1, col );            
         }    
     }
      fclose(sal); 
}


//inicializamos todo, y decimos de que archivo se van a buscar las palabras
int main (){
  int numPalabrasPtr = 0;
  int i=0;
  char **array2d;
  //numPalabrasPtr se pasa por referencia, para saber cuantas lineas tiene el archivo
  array2d  = lee(&numPalabrasPtr, "entrada.txt"); 
  char *palabrasBusca[] = {"gato", "perro", "raton", "elefante", "rino", "serpiente", "pescadito"};
  //de q tamaño es la lista??
  int len = sizeof(palabrasBusca) / sizeof(char *) ;
  //buscamos cada palabra en nuestra matriz de palabras
  for(i =0; i< len; i++){
        busca(numPalabrasPtr,array2d, palabrasBusca[i] );
  }
  getch();
  return 0;
}

el archivo entrada usado fue:

aaaaaaaaaaaaaaaaaaaaa
setrsdfdsrrtdpyrinoeq
weraderelefantewwerrr
trtevhjujhaspescadito
rtxvfdghhgperrodrdvbh
ifghhfgaaasdserpiente
naendsdsadsasafrrsdft
nssdofgfgghghghdddddd
ttegatovvvfgfyhgggggg
rrrrrrrrrrrrrrrrrrrrr
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.