Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #define ITEMS_OF(a) (sizeof(a)/sizeof(a[0]))
- void CalcLetterMap(const char *op, char *letters)
- {
- const char *p = op;
- while (*p)
- {
- letters[*p] = 1;
- ++p;
- }
- }
- int SameLetters(const char *op1, const char * op2)
- {
- char op1Letters[256], op2Letters[256];
- memset(op1Letters, 0, sizeof(op1Letters) / sizeof(op1Letters[0]));
- memset(op2Letters, 0, sizeof(op2Letters) / sizeof(op2Letters[0]));
- CalcLetterMap(op1, op1Letters);
- CalcLetterMap(op2, op2Letters);
- int ret = memcmp(op1Letters, op2Letters, sizeof(op1Letters));
- return ret == 0;
- }
- struct TEST_ENTRY
- {
- const char *op1;
- const char *op2;
- int ExpectedResult;
- }
- TestArray[] =
- {
- {"aaaaa", "a", 1},
- {"aaaaa", "b", 0},
- {"abababc", "cba", 1},
- {"abcdef", "abcdef", 1},
- {"abcdef", "abcde", 0},
- {"aaabbbccc888", "88888aaaaa88888aaaaa88888aaa888888aaaaaaabbbbbbbcccbbccbbc", 1},
- {"a", "a", 1},
- {"ab", "ba", 1},
- {"a", "b", 0},
- {"444444444444", "555555555555555555", 0},
- {"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", "QWERTYUIOPLKJHGFDSAZXCVBNM0987654321mnbvcxzasdfghjklpoiuytrewq", 1},
- {"xa ax", "Xa ax", 0},
- };
- int main(void)
- {
- for (unsigned int testIndex = 0; testIndex < ITEMS_OF(TestArray); ++testIndex)
- {
- struct TEST_ENTRY * pTest = &TestArray[testIndex];
- int result = SameLetters(pTest->op1, pTest->op2);
- printf("%s: SameLetters(%s,%s)\n", (result != pTest->ExpectedResult) ? "FAIL" : "PASS", pTest->op1, pTest->op2);
- result = SameLetters(pTest->op2, pTest->op1);
- printf("%s: SameLetters(%s,%s)\n", (result != pTest->ExpectedResult) ? "FAIL" : "PASS", pTest->op2, pTest->op1);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement