Skip to content

Commit

Permalink
Add palindrome in C
Browse files Browse the repository at this point in the history
  • Loading branch information
oboukli committed Jun 27, 2023
1 parent c4713c6 commit a372085
Show file tree
Hide file tree
Showing 6 changed files with 208 additions and 2 deletions.
26 changes: 25 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

cmake_minimum_required(VERSION 3.26)

set(CMAKE_C_STANDARD 90)
set(CMAKE_C_STANDARD_REQUIRED ON)
set(CMAKE_C_EXTENSIONS OFF)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
Expand All @@ -17,7 +21,7 @@ set(

project(
ForFun
LANGUAGES CXX
LANGUAGES C CXX
)

add_library(
Expand Down Expand Up @@ -45,6 +49,21 @@ target_include_directories(

link_libraries(forfun)

add_library(
forfun_c
SHARED
"include/forfun/palindrome.h"
"src/palindrome.c"
)

target_include_directories(
forfun_c
PUBLIC
"${CMAKE_SOURCE_DIR}/include"
)

link_libraries(forfun_c)

add_executable(
fizzbuzz
"test/fizzbuzz.cpp"
Expand All @@ -60,6 +79,11 @@ add_executable(
"test/palindrome.cpp"
)

add_executable(
palindrome_c
"test/palindrome.c"
)

add_executable(
palindromic_number
"test/palindromic_number.cpp"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Miscellaneous coding problem solutions

Solutions written in C++ (C++17 and C++20) for various coding problems.
Solutions for various coding problems, written in C90, C++17, and C++20.

The solutions are not necessarily optimal or meant as best practice.

Expand Down
17 changes: 17 additions & 0 deletions benchmark/palindrome_benchmark.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

#include <string_view>

#include "forfun/palindrome.h"
#include "forfun/palindrome.hpp"

TEST_CASE("forfun::palindrome benchmarking") {
Expand All @@ -24,6 +25,14 @@ TEST_CASE("forfun::palindrome benchmarking") {

.title("Palindrome (case-sensitive)")

.run(
NAMEOF_RAW(::is_palindrome).c_str(),
[&palindrome]() {
auto r{
::is_palindrome(palindrome.data(), palindrome.size())};
ankerl::nanobench::doNotOptimizeAway(r);
})

.run(
NAMEOF_RAW(forfun::palindrome::fast::is_palindrome).c_str(),
[&palindrome]() {
Expand Down Expand Up @@ -63,6 +72,14 @@ TEST_CASE("forfun::palindrome benchmarking") {

.title("Palindrome (case-insensitive)")

.run(
NAMEOF_RAW(::is_palindrome_ci).c_str(),
[&palindrome]() {
auto r{::is_palindrome_ci(
palindrome.data(), palindrome.size())};
ankerl::nanobench::doNotOptimizeAway(r);
})

.run(
NAMEOF_RAW(forfun::palindrome::fast::is_palindrome_ci).c_str(),
[&palindrome]() {
Expand Down
31 changes: 31 additions & 0 deletions include/forfun/palindrome.h
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_ */
40 changes: 40 additions & 0 deletions src/palindrome.c
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;
}
94 changes: 94 additions & 0 deletions test/palindrome.c
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;
}

0 comments on commit a372085

Please sign in to comment.