Advertisement
Guest User

Untitled

a guest
Aug 24th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.99 KB | None | 0 0
  1. char * getCharId(int id)
  2. {
  3.     char *charId = (char *)malloc(len[i]);
  4.  
  5.         int t = ids[i], j = 0, l = len[i];
  6.         while (--l >= 0)
  7.         {
  8.             charIds[i][l] = t % 10;
  9.             t /= 10;
  10.         }
  11. }
  12.  
  13. void locateIds(int *ids, int size)
  14. {
  15.     int *count = (int*)calloc(size * sizeof(int));
  16.     char **charIds = (char **)malloc(size * sizeof(char *));
  17.     char **pos = (char**)calloc(size * sizeof(char *));
  18.     char c;
  19.     int i;
  20.  
  21.     // precalculate character ids
  22.     for (i = 0; i < size; i++)
  23.     {
  24.         pos[i] = charIds[i] = getCharId(ids[i]);
  25.     }
  26.  
  27.     while ((c = nextChar()) != EOF)
  28.     {
  29.         for (int i = 0; i < size; i++)
  30.         {
  31.             char *id = charIds[i];
  32.             if (*pos[i] == c)
  33.             {
  34.                 if (*(++pos[i]) == '\0')
  35.                 {
  36.                     pos[i] = id;
  37.                     count[i]++;
  38.                 }
  39.             }
  40.             else
  41.             {
  42.                 pos[i] = id;
  43.             }
  44.         }
  45.     }
  46.  
  47.     // print matches
  48.     for (i = 0; i < size; i++)
  49.     {
  50.         printf("%d: %d\n", ids[i], count[i]);
  51.     }
  52.  
  53.     // cleanup
  54.     free(pos);
  55.     free(count);
  56.     for (i = 0; i < size; i++)
  57.     {
  58.         free(charIds[i]);
  59.     }
  60.     free(charIds);
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement