Advertisement
Guest User

Untitled

a guest
Nov 15th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.93 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. // aab
  5. // aba <- OK
  6. // abb
  7. // aab <- WRONG
  8.  
  9. int cmpr(char *str1, char *str2) {
  10.  
  11.     if (str1 == NULL || str2 == NULL) {
  12.         return 0;
  13.     }
  14.    
  15.     //declarations and initializations of 26-elements arrays,
  16.     //every single alphabetic character with one index assigned
  17.     int tab1[26] = {0}, tab2[26] = {0};
  18.     int i = 0, j = 0;
  19.    
  20.     //incrementation of specific array's index for str1
  21.     for (; *(str1 + i) != '\0'; ++i) {
  22.         tab1[*(str1 + i) - 'a']++;
  23.     }
  24.    
  25.     //incrementation of specific array's index for str2
  26.     for (; *(str2 + j) != '\0'; ++j) {
  27.         tab2[*(str2 + j) - 'a']++;
  28.     }  
  29.    
  30.     //validation of both arrays length and memory areas
  31.     if (i == j) {
  32.         if (!memcmp(tab1, tab2, sizeof(tab1))) {
  33.             return 1;
  34.         }
  35.     }
  36.     return 0;
  37. }
  38.  
  39. int main() {
  40.     printf("%d\n", cmpr("abcdef", "abcfed"));
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement