Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <setjmp.h>
- #include <string.h>
- #include <stdbool.h>
- #define NULLSTRING 1
- #define EMPTYSTRING 2
- jmp_buf buf;
- int exception_number;
- #define TRY if ((exception_number = setjmp(buf)) == 0)
- #define CATCH(x) else if (exception_number == x)
- #define CATCHALL else
- #define THROW(ex) (longjmp(buf, ex))
- _Bool palindrome(const char *str)
- {
- if (str == NULL)
- THROW(NULLSTRING);
- size_t len = strlen(str);
- if (len == 0)
- THROW(EMPTYSTRING);
- size_t i;
- for (i = 0; i <= len / 2; i++)
- {
- if (str[i] != str[len - i - 1])
- return false;
- }
- return true;
- }
- int main()
- {
- TRY
- {
- printf("%d", palindrome("saa"));
- }
- CATCH(NULLSTRING)
- {
- printf("%s", "NullString!!!\n");
- }
- CATCH(EMPTYSTRING)
- {
- printf("%s", "EmptyString!!!\n");
- }
- CATCHALL
- {
- printf("%s", "unrecognized exception!\n");
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment