diff --git a/CMakeLists.txt b/CMakeLists.txt index 5cd50dd..3a4dbfc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,7 +3,7 @@ cmake_minimum_required(VERSION 3.0) project(learningc LANGUAGES C) set(CHAPTER 1) -set(EXERCISE 21) +set(EXERCISE 22) add_executable(learningc chapters/${CHAPTER}/exercises/${EXERCISE}.c) diff --git a/chapters/1/exercises/21.c b/chapters/1/exercises/21.c index d7b141b..0f7b622 100644 --- a/chapters/1/exercises/21.c +++ b/chapters/1/exercises/21.c @@ -1,4 +1,4 @@ -#include +#include #include "shared/limits.h" #include "shared/functions.h" #include "shared/functions/entab.c" diff --git a/chapters/1/exercises/22.c b/chapters/1/exercises/22.c new file mode 100644 index 0000000..ec40317 --- /dev/null +++ b/chapters/1/exercises/22.c @@ -0,0 +1,12 @@ +#include +#include "shared/functions.h" +#include "shared/functions/fold.c" + +int main() { + int len; + char line[MAXLINE]; + while ((len = fold(line,MAXLINE, 30)) > 0) { + printf("%s", line); + } + return 0; +} diff --git a/chapters/1/exercises/shared/functions.h b/chapters/1/exercises/shared/functions.h index bc956bb..498798b 100644 --- a/chapters/1/exercises/shared/functions.h +++ b/chapters/1/exercises/shared/functions.h @@ -6,3 +6,4 @@ void copy(char to[], char from[]); int getlines(char line[], int maxline); int detab(char s[],int lim, int col); int entab(char s[], int lim, int col); +int fold(char s[], int lim, int cpr); diff --git a/chapters/1/exercises/shared/functions/fold.c b/chapters/1/exercises/shared/functions/fold.c new file mode 100644 index 0000000..4062aeb --- /dev/null +++ b/chapters/1/exercises/shared/functions/fold.c @@ -0,0 +1,19 @@ +#include + +int fold(char s[], int lim, int cpr) { + int c, i; + for (i=0; i < lim-1 && i 0 && (s[i-1] == ' ' || s[i-1] == '\t'); i--); + if (i > 0) { + s[i] = '\n'; + s[i+1] = '\0'; + } else { + s[i] = '\0'; + } + return i; +}