Searching word(string) in matrix.

This is a discussion on Searching word(string) in matrix. within the C Programming forums, part of the General Programming Boards category; Hi, I'm programming an alphabeth soup, i got my 25x25 matrix displayed with random letters, i've been thinking and thinking ...

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    5

    Searching word(string) in matrix.

    Hi, I'm programming an alphabeth soup, i got my 25x25 matrix displayed with random letters, i've been thinking and thinking different ways to search words o it but neither of them works.
    ex

    a j k k l m
    a f q w r s
    m i t u v w

    i want to get the word "AJK", i tried by using multiple for's and if's but i still couldn't get it, and after i started looking for functions like strstr, some help would be great i'm new at programming.
    sorry about the english.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Do you have a list of words? Your computer can't tell if a word has been found, unless you tell it what "words" are.

    Then you can search your matrix (in all 8 directions), but you have to be careful to stay within the bounds of the matrix. I suggest using a 1 row and 1 column "border", and give it a very unique "border" value - maybe -99. Now you can use your checking for letters more freely, and stop (checking letters in that direction), if you detect that value in the matrix.

    But first thing, is you need a word list - they are available on-line, (big and medium one's), but you may use a small one smartly, for starting out, and put each word, into an array - for now.

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    5
    you get the word by scanf

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I see, you're looking for ONE word, that the user enters.

    So you need to search outward, from every letter in the array. That sounds like a for loop to me:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    #define Nrows 25
    #define Ncols 25
    
    
    int len, r,c; 
    char word[4]="ajk", *pfound;
    char found[Ncols+1];
    
    
    scanf("%s", word);
    getchar();
    len = strlen(word);
    
    /* build a string from each row. See if the string contains the word */
    for(r=0;r<Nrows;r++) {
      found[0]='\0';                 //reset the found array
      for(c=0;c<Ncols;c++) {  //build the string from the row of letters
         found[c]=letters[r][c];
      }  
       found[c]='\0'; //add end of string char
       pfound = strstr(word, found);  //is the word inside the letters row?
    
       if(pfound) {    
         printf("\nWord was found. Word: %s   Found: %s", word, pfound);  //yes
        }
      }
    }
    This isn't a program that will run. It's "idea code", that shows how I'd go about it. This is for checking rows only, not columns, and not 8 directions. Obviously, you can adapt this same idea, to check all the directions.

  5. #5
    Registered User
    Join Date
    Sep 2010
    Posts
    5
    thanks dude that was exactly what i was looking for .

  6. #6
    Registered User
    Join Date
    Sep 2010
    Posts
    5
    ok i tried what you said, but my code still crashes, i don't get why, this is the general code. (sorry its in spanish)
    the switch is a menu where you
    1 load soup
    2 show soup
    3 search word
    4 exit

    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<stdlib.h>
    #include<time.h>
    #include<string.h>
    #define n 25
    
    void main ()
    {
        char sopa[n][n], palabra[26], busca[n+1],*encuentra ;
        int menu,i,j,letras;
        printf("Bienvenido, tipee el numero de la opcion que desea\n");
    
         do
        {
    
            printf("\n1.-Cargar sopa\n2.-Mostrar sopa\n3.-Buscar palabra\n4.-Salir\n");
            scanf("%d",&menu);
    
    
            switch(menu)
            {
                case 1:
                    srand(time(NULL));
                    for(i=1;i<n;i++)
                    {
                        for(j=1;j<n;j++)
                        sopa[i][j]=(rand()%26)+97;
                    };
                    printf("NUEVA SOPA CARGADA");
                    break;
                case 2:
                    printf("SOPA DE LETRAS\n");
                    printf("--------------------\n");
                    for(i=0;i<n;i++)
                    {
                        for(j=0;j<n;j++)
                        if(j==n-1)
                        printf("%c\n",sopa[i][j]);
                        else
                        printf("%c ",sopa[i][j]);
                    }
                    break;
                case 3:
                    printf("ingrese la palabra que desea buscar y presione enter. \n");
                    scanf("%s",palabra);
                    printf("Palabra a buscar:\n");
                    printf("%s",palabra);
                    letras=strlen(palabra);
                    printf("\nPalabra de %d letras",letras);
                    printf("\nBuscando...\n");
    
    
                    for(i=0;i<n;i++)
                    {
                        busca[0]='\0';
                        for(j=0;j<n;j++)
                        {
                           busca[i]=sopa[i][j];
                        }
                        encuentra=strstr(palabra,busca);
                         if(encuentra=palabra)
                        {
                            printf("\nLa palabra fue encontrada:\nPalabra:%s\nFila:%d",palabra,i);
                            encuentra[0]='\0';
                        }
                        else
                        printf("\nPalabra no encontrada :(");
    
                    }
    
    
                    break;
    
                case 4:
                    printf("Cerrando aplicacion");break;
                default:
                    printf("Entrada invalida, vuelva a intentar.");
                    break;
            }
        }
        while(menu!=4);
    }

    no matter what i try in search word it doesn't seems to perfectly work.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The first step in troubleshooting is to see where it "doesn't work perfectly". Give me an example of when it fails.

    Any warnings or error messages?

    I'd also suggest you paste this into the Google translate website window, and have it translated to english. Then copy and paste it back here - the little spanish I knew, has seriously degraded over the decades.

  8. #8
    Registered User
    Join Date
    Sep 2010
    Posts
    5
    i finally did it thanks for all adak you were very helpfull!
    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<stdlib.h>
    #include<time.h>
    #include<string.h>
    #define n 25
    void cargar(char sopa[n][n])
    {
        int i,j;
        srand(time(NULL));
        for(i=0;i<n;i++)
        {
            for(j=0;j<n;j++)sopa[i][j]=(rand()%26)+97;
        }
        printf("NUEVA SOPA CARGADA");
    
    }
    void mostrar(char sopa[n][n])
    {
        int i,j;
        printf("SOPA DE LETRAS\n");
        printf("--------------------\n\n\n");
        for(i=0;i<n;i++)
        {
            for(j=0;j<n;j++)
            {if(j==n-1)
            printf("%c\n",sopa[i][j]);
            else
            printf("%c ",sopa[i][j]);
            }
        }
    }
    void invertir(char palabra[])
    {
      int aux, i, j;
    
      for (i=0, j=strlen(palabra)-1; i<j;i++, j--)
      {
        aux= palabra[i];
        palabra[i] =palabra[j];
        palabra[j] =aux;
      }
    }
    
    int buscacol(char palabra[n],char sopa[n][n])
    {
      int i,j,aux=0,aux2,encuentra,coord[2];
      for(i=1;i<n;i++)
        {
            for(j=1;j<n;j++)
            {
                if(palabra[aux]==sopa[i][j])
                {
                    for(aux2=1,aux=0;palabra[aux]!='\0' && aux2;aux++)
                    {
                        if(palabra[aux]==sopa[i][j+aux])
                        {
                            coord[0]=i;
                            coord[1]=j;
                            encuentra=1;
                        }
                        else
                        {
                            aux2=0;
                            aux=-1;
                        }
                    }
                    if (palabra[aux]=='\0') return (encuentra);
                }
            }
        }
      if(encuentra=1)
      {
          printf("Palabra encontrada en fila:%d Columna:%d",coord[0],coord[1]);
      }
    }
    
    int buscafil(char palabra[n],char sopa[n][n])
    {
      int i,j,aux=0,aux2,encuentra,coord[2];
      for(j=1;j<n;j++)
        {
            for(i=1;i<n;i++)
            {
                if(palabra[aux]==sopa[i][j])
                {
                    for(aux2=1,aux=0;palabra[aux]!='\0' && aux2;aux++)
                    {
                        if(palabra[aux]==sopa[i][j+aux])
                        {
                            coord[0]=i;
                            coord[1]=j;
                            encuentra=1;
                        }
                        else
                        {
                            aux2=0;
                            aux=-1;
                        }
                    }
                    if (palabra[aux]=='\0') return (encuentra);
                }
            }
        }
      if(encuentra=1)
      {
          printf("Palabra encontrada en fila:%d Columna:%d",coord[0],coord[1]);
      }
    }
    
    
    
    
    void main ()
    {
        char sopa[n][n], palabra[n];
        int menu,i,j,h,aux;
    
    
        printf("Bienvenido, tipee el numero de la opcion que desea\n");
    
         do
        {
    
            printf("\n1.-Cargar sopa\n2.-Mostrar sopa\n3.-Buscar palabra\n4.-Salir\n");
            scanf("%d",&menu);
    
    
            switch(menu)
            {
                case 1:
                    cargar(sopa);
                    break;
                case 2:
                    mostrar(sopa);
                    break;
    
                case 3:
                    printf("ingrese la palabra que desea buscar y presione enter. \n");
                    scanf("%s",palabra);
                    printf("Palabra a buscar:\n");
                    printf("%s",palabra);
                    h=strlen(palabra);
                    printf("\nPalabra de %d letras",h);
                    printf("\nBuscando...\n");
                    for(aux=0;aux<1;0)
                    {
    
                        if(buscacol(palabra,sopa))
                        break;
                        else{aux++;}
                        invertir(palabra);
                        if(buscacol(palabra,sopa))
                        break;
                        else{aux++;}
                        invertir(palabra);
                        if(buscafil(palabra,sopa))
                        break;
                        else{aux++;}
                        invertir(palabra);
                        if(buscafil(palabra,sopa))
                        break;
                        else{aux++;}
                    }
                    if(aux=4)
                    printf("\nPalabra no encontrada, pruebe con otra.\n");
    
    
                    break;
    
                case 4:
                    printf("\nCerrando aplicacion\n");break;
                default:
                    printf("\nEntrada invalida, vuelva a intentar.\n");
                    break;
            }
    
        }
        while(menu!=4);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting Matrix
    By alex 2010 in forum C++ Programming
    Replies: 0
    Last Post: 06-24-2010, 10:40 AM
  2. Matrix Help
    By HelpmeMark in forum C++ Programming
    Replies: 27
    Last Post: 03-06-2008, 05:57 PM
  3. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 01:13 AM
  4. Visual C++ 2005 linking and file sizes
    By Rune Hunter in forum C++ Programming
    Replies: 2
    Last Post: 11-12-2005, 10:41 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21