Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.88 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. #define ITEMS_OF(a) (sizeof(a)/sizeof(a[0]))
  5.  
  6. void CalcLetterMap(const char *op, char *letters)
  7. {
  8. const char *p = op;
  9.  
  10. while (*p)
  11. {
  12. letters[*p] = 1;
  13. ++p;
  14. }
  15. }
  16.  
  17. int SameLetters(const char *op1, const char * op2)
  18. {
  19. char op1Letters[256], op2Letters[256];
  20.  
  21. memset(op1Letters, 0, sizeof(op1Letters) / sizeof(op1Letters[0]));
  22. memset(op2Letters, 0, sizeof(op2Letters) / sizeof(op2Letters[0]));
  23.  
  24. CalcLetterMap(op1, op1Letters);
  25. CalcLetterMap(op2, op2Letters);
  26.  
  27. int ret = memcmp(op1Letters, op2Letters, sizeof(op1Letters));
  28.  
  29. return ret == 0;
  30. }
  31.  
  32. struct TEST_ENTRY
  33. {
  34. const char *op1;
  35. const char *op2;
  36. int ExpectedResult;
  37. }
  38. TestArray[] =
  39. {
  40. {"aaaaa", "a", 1},
  41. {"aaaaa", "b", 0},
  42. {"abababc", "cba", 1},
  43. {"abcdef", "abcdef", 1},
  44. {"abcdef", "abcde", 0},
  45. {"aaabbbccc888", "88888aaaaa88888aaaaa88888aaa888888aaaaaaabbbbbbbcccbbccbbc", 1},
  46. {"a", "a", 1},
  47. {"ab", "ba", 1},
  48. {"a", "b", 0},
  49. {"444444444444", "555555555555555555", 0},
  50. {"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", "QWERTYUIOPLKJHGFDSAZXCVBNM0987654321mnbvcxzasdfghjklpoiuytrewq", 1},
  51. {"xa ax", "Xa ax", 0},
  52. };
  53.  
  54. int main(void)
  55. {
  56.  
  57. for (unsigned int testIndex = 0; testIndex < ITEMS_OF(TestArray); ++testIndex)
  58. {
  59. struct TEST_ENTRY * pTest = &TestArray[testIndex];
  60.  
  61. int result = SameLetters(pTest->op1, pTest->op2);
  62. printf("%s: SameLetters(%s,%s)\n", (result != pTest->ExpectedResult) ? "FAIL" : "PASS", pTest->op1, pTest->op2);
  63.  
  64. result = SameLetters(pTest->op2, pTest->op1);
  65. printf("%s: SameLetters(%s,%s)\n", (result != pTest->ExpectedResult) ? "FAIL" : "PASS", pTest->op2, pTest->op1);
  66.  
  67. }
  68.  
  69. return 0;
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement