Advertisement
Falexom

Untitled

Dec 11th, 2021
817
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.86 KB | None | 0 0
  1. #include <ctype.h>
  2. #include <stdbool.h>
  3.  
  4. #include <stdlib.h>
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <math.h>
  8. #include <sys/mman.h>
  9. #include <sys/types.h>
  10. #include <sys/stat.h>
  11. #include <fcntl.h>
  12. #include <unistd.h>
  13. #include <errno.h>
  14. #include <time.h>
  15.  
  16. #include "plugin_api.h"
  17.  
  18. static char *g_lib_name = "libavg.so";
  19.  
  20. static char *g_plugin_purpose = "Check whether two strings are anagram of each other";
  21.  
  22. static char *g_plugin_author = "Author";
  23.  
  24. #define NO_OF_CHARS 256
  25.  
  26. /* function to check whether two strings are anagram of
  27.    each other */
  28. bool areAnagram(char* str1, char* str2)
  29. {
  30.     // Create 2 count arrays and initialize all values as 0
  31.     int count1[NO_OF_CHARS] = { 0 };
  32.     int count2[NO_OF_CHARS] = { 0 };
  33.     int i;
  34.  
  35.     // For each character in input strings, increment count
  36.     // in the corresponding count array
  37.     for (i = 0; str1[i] && str2[i]; i++) {
  38.         count1[str1[i]]++;
  39.         count2[str2[i]]++;
  40.     }
  41.  
  42.     // If both strings are of different length. Removing
  43.     // this condition will make the program fail for strings
  44.     // like "aaca" and "aca"
  45.     if (str1[i] || str2[i])
  46.         return false;
  47.  
  48.     // Compare count arrays
  49.     for (i = 0; i < NO_OF_CHARS; i++)
  50.         if (count1[i] != count2[i])
  51.             return false;
  52.  
  53.     return true;
  54. }
  55.  
  56. /* Driver code*/
  57. int main()
  58. {
  59.     char str1[] = "geeksforgeeks";
  60.     char str2[] = "forgeeksgeeks";
  61.    
  62.     // Function Call
  63.     if (areAnagram(str1, str2))
  64.         printf("The two strings are anagram of each other");
  65.     else
  66.         printf("The two strings are not anagram of each "
  67.                "other");
  68.  
  69.     return 0;
  70. }
  71.  
  72. static int g_po_arr_len = sizeof(g_po_arr)/sizeof(g_po_arr[0]);
  73.  
  74.  
  75.  
  76. int plugin_process_file(const char *fname,
  77.         struct option in_opts[],
  78.         size_t in_opts_len) {
  79.  
  80.     // Return error by default 
  81.     //int ret = -1;     // Это должно быть, но в данном случае без этого можно обойтись. В ваших лабораторных эта строка быть должна!
  82.    
  83.     // Pointer to file mapping
  84.     //unsigned char *ptr = NULL;  //Тоже нужная строчка - при правильной реализации (с точки зрения задания).
  85.    
  86.     char *DEBUG = getenv("LAB1DEBUG");
  87.    
  88.     if (!fname || !in_opts || !in_opts_len) {
  89.         errno = EINVAL;
  90.         return -1;
  91.     }
  92.    
  93.     if (DEBUG) {
  94.         for (size_t i = 0; i < in_opts_len; i++) {
  95.             fprintf(stderr, "DEBUG: %s: Got option '%s' with arg '%s'\n",
  96.                 g_lib_name, in_opts[i].name, (char*)in_opts[i].flag);
  97.         }
  98.     }
  99.    
  100.     int leng = 0;
  101.     int got_leng = 0;
  102.     int saved_errno = 0;
  103.  
  104.  
  105.  
  106.         #define OPT_CHECK(opt_var, is_double) \
  107.     if (got_##opt_var) { \
  108.         if (DEBUG) { \
  109.             fprintf(stderr, "DEBUG: %s: Option '%s' was already supplied\n", \
  110.                 g_lib_name, in_opts[i].name); \
  111.         } \
  112.         errno = EINVAL; \
  113.         return -1; \
  114.     } \
  115.     else { \
  116.         char *endptr = NULL; \
  117.         opt_var = is_double ? \
  118.                     strtod((char*)in_opts[i].flag, &endptr): \
  119.                     strtol((char*)in_opts[i].flag, &endptr, 0); \
  120.         if (*endptr != '\0') { \
  121.             if (DEBUG) { \
  122.                 fprintf(stderr, "DEBUG: %s: Failed to convert '%s'\n", \
  123.                     g_lib_name, (char*)in_opts[i].flag); \
  124.             } \
  125.             errno = EINVAL; \
  126.             return -1; \
  127.         } \
  128.         got_##opt_var = 1; \
  129.     }
  130.     for (size_t i = 0; i < in_opts_len; i++) {
  131.         if (!strcmp(in_opts[i].name, OPT_LENG_STR)) {
  132.             OPT_CHECK(leng, 0)
  133.         }
  134.     else {
  135.             errno = EINVAL;
  136.             return -1;
  137.         }
  138.     }
  139.    
  140.  
  141.  
  142.     // C program to check if two strings
  143. // are anagrams of each other
  144.  
  145.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement