Add CMakeLists.txt and answer for problem 1

This commit is contained in:
Madeline Busig 2024-10-29 23:41:42 -07:00
parent 4c01a13684
commit b345ff63cc
4 changed files with 54 additions and 0 deletions

14
CMakeLists.txt Normal file
View File

@ -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}")

7
include/p1.h Normal file
View File

@ -0,0 +1,7 @@
#ifndef P1_H
#define P1_H
int count_alphanumeric(const char* str);
#endif

14
src/main.c Normal file
View File

@ -0,0 +1,14 @@
#include <stdio.h>
#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;
}

19
src/p1.c Normal file
View File

@ -0,0 +1,19 @@
#include "p1.h"
#include <ctype.h>
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;
}