-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
208 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
Copyright (c) Omar Boukli-Hacene. All rights reserved. | ||
Distributed under an MIT-style license that can be | ||
found in the LICENSE file. | ||
*/ | ||
|
||
/* SPDX-License-Identifier: MIT */ | ||
|
||
/* | ||
Problem reference: | ||
https://en.wikipedia.org/wiki/Palindrome | ||
*/ | ||
|
||
#ifndef FORFUN_PALINDROME_H_ | ||
#define FORFUN_PALINDROME_H_ | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif /* __cplusplus */ | ||
|
||
#include <stddef.h> | ||
|
||
int is_palindrome(char const* const str, size_t const size); | ||
|
||
int is_palindrome_ci(char const* const str, size_t const size); | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif /* __cplusplus */ | ||
|
||
#endif /* FORFUN_PALINDROME_H_ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
Copyright (c) Omar Boukli-Hacene. All rights reserved. | ||
Distributed under an MIT-style license that can be | ||
found in the LICENSE file. | ||
*/ | ||
|
||
/* SPDX-License-Identifier: MIT */ | ||
|
||
#include "forfun/palindrome.h" | ||
|
||
#include <ctype.h> | ||
|
||
int is_palindrome(char const* const str, size_t const size) { | ||
size_t const end = size - 1; | ||
size_t const mid = size / 2; | ||
|
||
size_t i; | ||
for (i = 0; i < mid; ++i) { | ||
if (str[i] != str[end - i]) { | ||
return 0; | ||
} | ||
} | ||
|
||
return 1; | ||
} | ||
|
||
int is_palindrome_ci(char const* const str, size_t const size) { | ||
size_t const end = size - 1; | ||
size_t const mid = size / 2; | ||
|
||
size_t i; | ||
for (i = 0; i < mid; ++i) { | ||
if (tolower((unsigned char)str[i]) | ||
!= tolower((unsigned char)str[end - i])) { | ||
return 0; | ||
} | ||
} | ||
|
||
return 1; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/* | ||
Copyright (c) Omar Boukli-Hacene. All rights reserved. | ||
Distributed under an MIT-style license that can be | ||
found in the LICENSE file. | ||
*/ | ||
|
||
/* SPDX-License-Identifier: MIT */ | ||
|
||
#include "forfun/palindrome.h" | ||
|
||
#include <assert.h> | ||
#include <string.h> | ||
|
||
void test_palindrome() { | ||
{ | ||
char const* const s = ""; | ||
size_t const len = strlen(s); | ||
int const f_palindrome = is_palindrome(s, len); | ||
assert(f_palindrome); | ||
} | ||
|
||
{ | ||
char const* const s = "\xb8Y\xb8"; | ||
size_t const len = strlen(s); | ||
int const f_palindrome = is_palindrome(s, len); | ||
assert(f_palindrome); | ||
} | ||
|
||
{ | ||
char const* const s = "aa"; | ||
size_t const len = strlen(s); | ||
int const f_palindrome = is_palindrome(s, len); | ||
assert(f_palindrome); | ||
} | ||
|
||
{ | ||
char const* const s = "aba"; | ||
size_t const len = strlen(s); | ||
int const f_palindrome = is_palindrome(s, len); | ||
assert(f_palindrome); | ||
} | ||
|
||
{ | ||
char const* const s = "a b a"; | ||
size_t const len = strlen(s); | ||
int const f_palindrome = is_palindrome(s, len); | ||
assert(f_palindrome); | ||
} | ||
|
||
{ | ||
char const* const s = "101"; | ||
size_t const len = strlen(s); | ||
int const f_palindrome = is_palindrome(s, len); | ||
assert(f_palindrome); | ||
} | ||
|
||
{ | ||
char const* const s = "tattarrattat"; | ||
size_t const len = strlen(s); | ||
int const f_palindrome = is_palindrome(s, len); | ||
assert(f_palindrome); | ||
} | ||
|
||
{ | ||
char const* const s = "dummy"; | ||
size_t const len = strlen(s); | ||
int const f_palindrome = is_palindrome(s, len); | ||
assert(f_palindrome == 0); | ||
} | ||
} | ||
|
||
void test_palindrome_ci() { | ||
{ | ||
char const* const s = "Aa"; | ||
size_t const len = strlen(s); | ||
int const f_palindrome = is_palindrome_ci(s, len); | ||
assert(f_palindrome); | ||
} | ||
|
||
{ | ||
char const* const s = "Aab4'{x{'4BaA"; | ||
size_t const len = strlen(s); | ||
int const f_palindrome = is_palindrome_ci(s, len); | ||
assert(f_palindrome); | ||
} | ||
} | ||
|
||
int main() { | ||
test_palindrome(); | ||
|
||
test_palindrome_ci(); | ||
|
||
return 0; | ||
} |