To find string in a matrix using c

To find string in a matrix using c

To find the string in a matrix from top to bottom and also from left to right.

Logic:

  • Convert the given string in an array format.
  • Then search the string from top to bottom.
  • And also search from left to right.
  • Then find the starting index and also ending index of string.

Algorithm:

  1. Get the input from user and stores it in the two-dimensional array.
  2. Then checks with the starting index of a searching array with the matrix.
  3. If the element is found the search with next index.
  4. Repeat step 3 until it reaches the last index of the matrix.

Program:

#include<stdio.h>
#include<math.h>
void main()
{
   //n->length of the string
   //q->row and column of the matrix
   //e->length of string to search in the matrix
   //p->stores square root value as float
   //l->stores square root value as integer
   //m->holds index of the matrix
   int i,j,k,n,m=0,q,l,r=0,e;
   char a[50],b[10][10],c[5];
   float p=0;
   clrscr();
   printf("enter the string:")
   gets(a);
   n=strlen(a);                                //finds the length of the string
   p=sqrt(n);                                  //finds the square root as a float value
   l=p;                                        //converts float to integer
   if(l==p)                                    //compare float and integer
   {
     q=l;
   }
   else
   {
     q=l+1;
   }
   for(i=0;i<q;i++)                          // stores the given string into the matrix
   {
     for(j=0;j<q;j++)
     {
        if(m<n)                             //checks with the length
        {
           b[i][j]=a[m];
           m++;
           printf("%c ",b[i][j]);
        }
      }printf("\n");
   }
   printf("enter the string to search:");
   gets(c);                               //string to search
   e=strlen(c);
   for(i=0;i<q;i++)                       //checks horizontally in matrix
   {
     k=0;                                 //initializing searching index
     for(j=0;j<q;j++)
     {
        if(b[i][j]==c[k])                //initial element of searching string checks with all elements in matrix
        {
          k++;                           //increments search string index
          r++;                           //counts the length of matching string
        }
        else                             //element not found
        {
           r=0;
        }
        if(r==e)                         //string found in matrix
        {
           printf("the string found at starting position:[%d,%d]",i,j-e+1);
           printf("and ends at position:[%d,%d]",i,j);
           break;
        }
     }
   }
   for(j=0;j<q;j++ )                   //checks vertically in matrix
   {
     k=0;                              //initializing searching index
     for(i=0;i<q;i++)
     {
       if(b[i][j]==c[k])              //initial element of searching string checks with all elements in matrix
       {
         k++;                         //increments search string index only if element matching
         r++;                         //counts the length of matching string
       }
       else
       {
       r=0;
       }
       if(r==e)                        //when string found in matrix
       {
          printf("the string found atstarting position:[%d,%d]",j,i-e+1);
          printf("and ends at position:[%d,%d]",j,i);
          break;
       }
     }
  }
   getch();
}

 

Vignesh

A Computer Science graduate who likes to make things simpler. When he’s not working, you can find him surfing the web, learning facts, tricks and life hacks. He also enjoys movies in his leisure time.

Leave a Reply

Your email address will not be published.

eighteen + nine =