From b345ff63cc4d0ee5aab2283f9c351da6dabb8b50 Mon Sep 17 00:00:00 2001 From: Madeline Busig Date: Tue, 29 Oct 2024 23:41:42 -0700 Subject: [PATCH] Add CMakeLists.txt and answer for problem 1 --- CMakeLists.txt | 14 ++++++++++++++ include/p1.h | 7 +++++++ src/main.c | 14 ++++++++++++++ src/p1.c | 19 +++++++++++++++++++ 4 files changed, 54 insertions(+) create mode 100644 CMakeLists.txt create mode 100644 include/p1.h create mode 100644 src/main.c create mode 100644 src/p1.c diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..49a2609 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,14 @@ +cmake_minimum_required(VERSION 3.5) + +project(practice_exam_2 C) +set(CMAKE_EXPORT_COMPILE_COMMANDS ON) + +add_executable(${PROJECT_NAME}) + +file(GLOB proj_sources LIST_DIRECTORIES false CONFIGURE_DEPENDS src/*.c) +set(proj_include "include") + +# Add sources to target +target_sources(${PROJECT_NAME} PRIVATE "${proj_sources}") +target_include_directories(${PROJECT_NAME} PRIVATE "${proj_include}") + diff --git a/include/p1.h b/include/p1.h new file mode 100644 index 0000000..93580d4 --- /dev/null +++ b/include/p1.h @@ -0,0 +1,7 @@ +#ifndef P1_H +#define P1_H + +int count_alphanumeric(const char* str); + +#endif + diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..4c49769 --- /dev/null +++ b/src/main.c @@ -0,0 +1,14 @@ +#include + +#include "p1.h" + +int main(void) { + // Problem 1 + int num_alphanum = count_alphanumeric("Hello World!!"); + printf("Number of alphanumeric characters in \"Hello World!!\": %d\n", num_alphanum); + + // Problem 2 + + return 0; +} + diff --git a/src/p1.c b/src/p1.c new file mode 100644 index 0000000..1f5c728 --- /dev/null +++ b/src/p1.c @@ -0,0 +1,19 @@ +#include "p1.h" + +#include + +int count_alphanumeric(const char* str) { + int num_alphanumeric = 0; + const char* current = str; + + while (*current != '\0') { + if (isalnum(*current)) { + ++num_alphanumeric; + } + + ++current; + } + + return num_alphanumeric; +} +