Initial commit of code for dopelib, and ch 1, 2, and 12

This commit is contained in:
2024-10-29 16:04:54 -04:00
parent 11601352d3
commit afbf4c2f7e
25 changed files with 745 additions and 0 deletions

View File

@@ -0,0 +1,5 @@
#pragma once
#include <iostream>
void reverse_str(char* str);

View File

@@ -0,0 +1,4 @@
chapter12_sources = ['reverse_string.cpp']
chapter12_lib = static_library('chapter12',
chapter12_sources)

View File

@@ -0,0 +1,20 @@
#include "chapter12.hpp"
#include <string.h>
#include <stdlib.h>
/*
Reverse String: Implement a function void reverse( char* str) in C or C++ which reverses
a null-terminated string.
*/
void reverse_str(char *str){
size_t len = strlen(str);
char *tmp = reinterpret_cast<char*>(malloc(len));
strcpy(tmp, str);
for(unsigned int offset = 0; offset < len; ++offset){
str[offset] = tmp[len-offset-1];
}
str[len] = '\0';
free(tmp);
}