Advertisement
Guest User

Untitled

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