Initial commit of code for dopelib, and ch 1, 2, and 12
This commit is contained in:
5
libs/chapter12/chapter12.hpp
Normal file
5
libs/chapter12/chapter12.hpp
Normal file
@@ -0,0 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
#include <iostream>
|
||||
|
||||
void reverse_str(char* str);
|
||||
4
libs/chapter12/meson.build
Normal file
4
libs/chapter12/meson.build
Normal file
@@ -0,0 +1,4 @@
|
||||
chapter12_sources = ['reverse_string.cpp']
|
||||
|
||||
chapter12_lib = static_library('chapter12',
|
||||
chapter12_sources)
|
||||
20
libs/chapter12/reverse_string.cpp
Normal file
20
libs/chapter12/reverse_string.cpp
Normal 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);
|
||||
}
|
||||
Reference in New Issue
Block a user