Pushok

Identifier by mask(FIX)

Apr 18th, 2011
450
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.86 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, yesno = 0;
  20. point:
  21.     system("cls");
  22.  
  23.     printf("Type string:\n");
  24.     gets(str);
  25.     printf("Type mask:\n");
  26.     gets(Mask);
  27.  
  28.     if((amount = charcount(str,'_') + 1) == 0) return;
  29.  
  30.     ch = (char**)malloc(sizeof(char *) * amount);
  31.     bck = (char**)malloc(sizeof(char *) * amount);
  32.     msk = (char**)malloc(sizeof(char *) * strlen(Mask));
  33.  
  34.     ch[0] = strtok(str, "_");
  35.  
  36.     if(amount > 1)
  37.     {
  38.         for(int i=1;i<amount;i++)
  39.         {
  40.             char* cc = strtok(NULL, "_");
  41.             if(cc != 0)
  42.                 ch[i] = cc;
  43.         }
  44.     }
  45.  
  46.     if(charcount(Mask,'*') > 0)
  47.     {
  48.         size = Mask[0] == '*' ? charcount(Mask, '*') : charcount(Mask, '*') + 1;
  49.         msk[0] = strtok(Mask, "*");
  50.  
  51.         if(size > 1)
  52.         {
  53.             for(int i=1, j = (int)(bool)msk[0];i<size;i++) {
  54.                 char* cc = strtok(NULL, "*");
  55.                 if(cc != 0)
  56.                 {
  57.                     ssize++;
  58.                     msk[j++] = cc;
  59.                 }
  60.             }
  61.             ssize++;
  62.         }
  63.     } else
  64.         msk[0] = Mask;
  65.  
  66.     for(int i=0, countToRight = 0;i < amount; i++, countToRight = 0)
  67.     {
  68.         for(int j=0;j<ssize;j++)
  69.         {
  70.             if(strstr(ch[i], msk[j]))
  71.                 countToRight++;
  72.         }
  73.  
  74.         if(countToRight >= ssize)
  75.         {
  76.             bck[counter++] = ch[i];
  77.         }
  78.     }
  79.  
  80.     if(counter != 0)
  81.     {
  82.         printf("Founds: ");
  83.         for(int i=0;i < counter;i++)
  84.             printf("%s %c ", bck[i], i == counter - 1 ? '\n' : '|');
  85.     } else {
  86.         printf("It is not contains.\n");
  87.     }
  88.  
  89.     free(ch);
  90.     free(bck);
  91.     free(msk);
  92.  
  93.     do
  94.     {
  95.         printf("\nDo you want to try it one more time? y/n : ");
  96.         yesno = tolower(getch());
  97.         printf("%c", yesno);
  98.         if(yesno == 'y')
  99.             goto point;
  100.     } while (yesno != 'y' && yesno != 'n');
  101. }
Advertisement
Add Comment
Please, Sign In to add comment