Pushok

Identifier by mask

Apr 17th, 2011
424
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.66 KB | None | 0 0
  1. #include "stdio.h"
  2. #include "stdlib.h"
  3. #include "conio.h"
  4. #include "iostream"
  5.  
  6. int charcount(const char* str, char ch)
  7. {
  8.     int count = 0;
  9.  
  10.     for(int i=0;i<strlen(str);i++)
  11.         if(str[i] == ch)
  12.             count++;
  13.  
  14.     return count;
  15. }
  16.  
  17. void main(void)
  18. {
  19.     char str[1024], Mask[16], **ch = 0, **bck = 0, **msk, counter = 0, amount = 0, size = 0, *cc = 0, ssize = 0;
  20.     printf("Type string:\n");
  21.     gets(str);
  22.     printf("Type mask:\n");
  23.     gets(Mask);
  24.  
  25.     if((amount = charcount(str,'_') + 1) == 0) return;
  26.  
  27.     ch = (char**)malloc(sizeof(char *) * amount);
  28.     bck = (char**)malloc(sizeof(char *) * amount);
  29.     msk = (char**)malloc(sizeof(char *) * strlen(Mask));
  30.  
  31.     ch[0] = strtok(str, "_");
  32.  
  33.     if(amount > 1)
  34.     {
  35.         for(int i=1;i<amount;i++)
  36.         {
  37.             char* cc = strtok(NULL, "_");
  38.             if(cc != 0)
  39.                 ch[i] = cc;
  40.         }
  41.     }
  42.  
  43.     if(charcount(Mask,'*') > 0)
  44.     {
  45.         size = Mask[0] == '*' ? charcount(Mask, '*') : charcount(Mask, '*') + 1;
  46.         msk[0] = strtok(Mask, "*");
  47.  
  48.         if(size > 1)
  49.         {
  50.             for(int i=1;i<size;i++) {
  51.                 char* cc = strtok(NULL, "*");
  52.                 if(cc != 0)
  53.                 {
  54.                     ssize++;
  55.                     msk[i] = cc;
  56.                 }
  57.             }
  58.         }
  59.     } else
  60.         msk[0] = Mask;
  61.  
  62.     for(int i=0, countToRight = false;i < amount; i++, countToRight = 0)
  63.     {
  64.         for(int j=0;j<ssize+1;j++)
  65.         {
  66.             if(strstr(ch[i], msk[j]))
  67.                 countToRight++;
  68.         }
  69.  
  70.         if(countToRight >= ssize+1)
  71.         {
  72.             bck[counter++] = ch[i];
  73.         }
  74.     }
  75.    
  76.     if(counter != 0)
  77.     {
  78.         printf("Founds:");
  79.         for(int i=0;i < counter;i++)
  80.             printf("%s %c ", bck[i], i == counter - 1 ? '\0' : '|');
  81.     }
  82.     free(ch);
  83.     free(bck);
  84.     free(msk);
  85.     getchar();
  86. }
  87.  
  88. //=Sample of work=
  89. //
  90. //Type string:
  91. //This_Is_So_Long_String
  92. //Type mask:
  93. //*o
  94. //Founds:So | Long
Advertisement
Add Comment
Please, Sign In to add comment