Update status to 1-22

This commit is contained in:
caskd 2019-10-21 19:14:22 +02:00
parent d7ad1d229b
commit fdf2e04a3f
No known key found for this signature in database
GPG Key ID: 79DB21404E300A27
5 changed files with 34 additions and 2 deletions

View File

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

View File

@ -1,4 +1,4 @@
#include<stdio.h>
#include <stdio.h>
#include "shared/limits.h"
#include "shared/functions.h"
#include "shared/functions/entab.c"

12
chapters/1/exercises/22.c Normal file
View File

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

View File

@ -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);

View File

@ -0,0 +1,19 @@
#include <stdio.h>
int fold(char s[], int lim, int cpr) {
int c, i;
for (i=0; i < lim-1 && i<cpr && (c=getchar())!=EOF && c!='\n';) {
if (i != 0 || (c != ' ' && c != '\t')) {
s[i] = c;
++i;
}
}
for (;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;
}