Advertisement
Idanref

Short String Inside Larger | C

Jan 4th, 2021
875
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.09 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <math.h>
  5.  
  6. void print_arr(int *arr, int n)
  7. {
  8.     printf("\n");
  9.  
  10.     printf("[");
  11.  
  12.     for(int i = 0; i < n-1; i++)
  13.     {
  14.         printf("%d, ", arr[i]);
  15.     }
  16.  
  17.     printf("%d]", arr[n-1]);
  18.  
  19.     printf("\n\n");
  20. }
  21.  
  22. int small_in_big(char *s1, char *s2) // s1 = small, s2 = big
  23. {
  24.     // small = jim
  25.     // big = mojair
  26.  
  27.     int new_small[26] = {};
  28.     int new_big[26] = {};
  29.  
  30.     int len_small = strlen(s1);
  31.     int len_big = strlen(s2);
  32.  
  33.     for(int i = 0; i < len_small; i++)
  34.         new_small[s1[i] - 97]++; // j = 106, index = 106-97 = 9
  35.  
  36.  
  37.     for(int j = 0; j < len_big; j++)
  38.         new_big[s2[j] - 97]++; // m = 109, index = 109-97 = 12
  39.  
  40.  
  41.     for(int k = 0; k < len_small; k++)
  42.     {
  43.         if(new_big[s1[k] - 97]) // new_big[9]
  44.             new_big[s1[k] - 97]--;
  45.  
  46.         else
  47.             return 0;
  48.     }
  49.  
  50.     return 1;
  51. }
  52.  
  53. void main()
  54. {
  55.     char str1[] = "mim";
  56.     char str2[] = "mojair";
  57.  
  58.     int result = small_in_big(str1, str2);
  59.  
  60.     printf("%d", result);
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement