1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Please Help me urgently

Discussion in 'C' started by shahzaib198, Aug 13, 2007.

  1. shahzaib198

    shahzaib198 New Member

    Joined:
    Aug 13, 2007
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Pattern Matching Code using table

    Below is my code, i m given an assignment of it. please help me.
    Well the problem that it is arrising is that the correct values after comparison are not coming. So please help me in this. in id[j].

    it should be like
    let say for pattern aaba
    Output would be
    Code:
          a           b
         aa         ab
         aaa       aab
         aaba      aabb
    
    no finding no is a problem for me
    it should be

    like
    Code:
          1         0
          2         0 
          2         3
          4         0
    
    
    but it is not coming
    please see the code

    Code:
    // i am labelling the area
    
    #include<stdio.h>
    #include<conio.h>
    #include<string.h>
    #include<ctype.h>
    
    #define SIZE strlen(pattern)
    
    char *sstring(char s[100],int i,int l);
    void remove_duplicates(void);
    void sublists(void);
    
    char column[20];
    char pattern[20];
    char sublist[21][20];
    char table[21][20];
    
    void main(void)
    {
    char string[100],temp[21][20],sl[20][20];
    int id[10][10];
    int count=0;
    char t[10];
    char *ptr;
    ptr=t;
    
    clrscr();
    
    //first of all getting the pattern
    printf("\n\nPlease Enter the Pattern : ");
    gets(pattern);
    
    //now making a copy of pattern for removing duplicates
    strcpy(column,pattern);
    
    //now removing duplicates
    remove_duplicates();
    
    //now making the sublist of the pattern
    sublists();
    
     for(int i=0;i<strlen(pattern);i++)
     {
      strcpy(temp[i],sstring(pattern,0,count++));
    }
    count=0;
    
    //this is the problem are
    for( i=0;i<strlen(pattern);i++)
    
      {
      for(int j=0;j<strlen(column);j++)
        {
           strcat(temp[i],sstring(column,count++,1));
    
           printf("%10s ",temp[i]);
    
           for(int l=strlen(pattern)-1;l>=0;l--)
           {
    	int counts=strlen(sublist[l])-1;   //2
    	int comps=strlen(temp[i]);         //1
    	int comp=0;
    	int cp=l;
    
    	    while(counts!=-1)
    	    {  counts=strlen(sublist[cp])-1;   //2
    	     if(temp[i][comp]==sublist[cp][counts])
    	      { count--;
    	       comp++;
    
    	       if(comp==comps)
    	       {
    		  id[i][j]=l;
    		  break;
    	       }
    	      }
    	     else
    		{
    		   cp--;
    		}
    
    	      }
    			cp=l;
    
    
           }
    
    //till here 
    
           if (count==strlen(column)) count=0;
    
    	//another loop for finding the position
    
           strcpy(temp[i],sublist[i]);
        }
      printf("\n");
    
      }
    
       printf("\n");
    
    
    
    //printing
    
    for(i=0;i<strlen(pattern);i++)
     {
     for(int j=0;j<strlen(column);j++)
      {
    
        printf("Q[%d]  ",id[i][j]);
    
       }
      printf("\n");
     }
    
    //printf("\n\nPlease Enter the string: ");
    //gets(string);
    
    
    getch();
    
    }
    
    
    
    void remove_duplicates(void)
    {
    
    //this is done to remove duplicates
    
      for(int j=SIZE-1;j>=0;j--)
       {
        for(int k=0;k<j;k++)
         {
          if (column[j]==column[k])
           {
    	column[j]='.';
           }
         }
       }
        for(int k=SIZE-1;k>=0;k--)
         {
    
          if (column[k]=='.')
           {
    	for(int i=k;i<SIZE;i++)
    	  column[i]=column[i+1];
           }
    
    }
    
    }
    
    
    
    
    
    void sublists(void)
    {
       int count=0;
    
    
    for(int i=0;i<strlen(pattern)+1;i++)
    {
      strcpy(sublist[i],sstring(pattern,0,count++));
    }
    
    sublist[0][0]='\0';
    for(i=0;i<strlen(pattern)+1;i++)
     printf("\n\nThe substring @ q[%d] is : %s ",i,sublist[i]);
       printf("\n\n");
    
    }
    
    
    
    
    char *sstring(char s[100],int i,int l)
    {
    int count;
    char sp[100];
    count=0;
     while (count<l)
     {
     sp[count]=s[i];
     count=count+1;
     i=i+1;
     }
     sp[count]='\0';
      return (sp);
    }
     
    Last edited by a moderator: Aug 13, 2007
  2. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:

Share This Page