Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ctype.h>
- #include <stdbool.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include <math.h>
- #include <sys/mman.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include <errno.h>
- #include <time.h>
- #include "plugin_api.h"
- static char *g_lib_name = "libavg.so";
- static char *g_plugin_purpose = "Check whether two strings are anagram of each other";
- static char *g_plugin_author = "Author";
- #define NO_OF_CHARS 256
- /* function to check whether two strings are anagram of
- each other */
- bool areAnagram(char* str1, char* str2)
- {
- // Create 2 count arrays and initialize all values as 0
- int count1[NO_OF_CHARS] = { 0 };
- int count2[NO_OF_CHARS] = { 0 };
- int i;
- // For each character in input strings, increment count
- // in the corresponding count array
- for (i = 0; str1[i] && str2[i]; i++) {
- count1[str1[i]]++;
- count2[str2[i]]++;
- }
- // If both strings are of different length. Removing
- // this condition will make the program fail for strings
- // like "aaca" and "aca"
- if (str1[i] || str2[i])
- return false;
- // Compare count arrays
- for (i = 0; i < NO_OF_CHARS; i++)
- if (count1[i] != count2[i])
- return false;
- return true;
- }
- /* Driver code*/
- int main()
- {
- char str1[] = "geeksforgeeks";
- char str2[] = "forgeeksgeeks";
- // Function Call
- if (areAnagram(str1, str2))
- printf("The two strings are anagram of each other");
- else
- printf("The two strings are not anagram of each "
- "other");
- return 0;
- }
- static int g_po_arr_len = sizeof(g_po_arr)/sizeof(g_po_arr[0]);
- int plugin_process_file(const char *fname,
- struct option in_opts[],
- size_t in_opts_len) {
- // Return error by default
- //int ret = -1; // Это должно быть, но в данном случае без этого можно обойтись. В ваших лабораторных эта строка быть должна!
- // Pointer to file mapping
- //unsigned char *ptr = NULL; //Тоже нужная строчка - при правильной реализации (с точки зрения задания).
- char *DEBUG = getenv("LAB1DEBUG");
- if (!fname || !in_opts || !in_opts_len) {
- errno = EINVAL;
- return -1;
- }
- if (DEBUG) {
- for (size_t i = 0; i < in_opts_len; i++) {
- fprintf(stderr, "DEBUG: %s: Got option '%s' with arg '%s'\n",
- g_lib_name, in_opts[i].name, (char*)in_opts[i].flag);
- }
- }
- int leng = 0;
- int got_leng = 0;
- int saved_errno = 0;
- #define OPT_CHECK(opt_var, is_double) \
- if (got_##opt_var) { \
- if (DEBUG) { \
- fprintf(stderr, "DEBUG: %s: Option '%s' was already supplied\n", \
- g_lib_name, in_opts[i].name); \
- } \
- errno = EINVAL; \
- return -1; \
- } \
- else { \
- char *endptr = NULL; \
- opt_var = is_double ? \
- strtod((char*)in_opts[i].flag, &endptr): \
- strtol((char*)in_opts[i].flag, &endptr, 0); \
- if (*endptr != '\0') { \
- if (DEBUG) { \
- fprintf(stderr, "DEBUG: %s: Failed to convert '%s'\n", \
- g_lib_name, (char*)in_opts[i].flag); \
- } \
- errno = EINVAL; \
- return -1; \
- } \
- got_##opt_var = 1; \
- }
- for (size_t i = 0; i < in_opts_len; i++) {
- if (!strcmp(in_opts[i].name, OPT_LENG_STR)) {
- OPT_CHECK(leng, 0)
- }
- else {
- errno = EINVAL;
- return -1;
- }
- }
- // C program to check if two strings
- // are anagrams of each other
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement