Last active
August 29, 2015 14:04
-
-
Save Learath2/ffea45fe6de3c9bcfb7d to your computer and use it in GitHub Desktop.
K&R2 Exercise 1-22
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*K&R2 Exercise 1-22 "fold"*/ | |
#include <stdio.h> | |
#define MAXLINE 1024 | |
#define FOLD 80 | |
int getline(char[], int); | |
int mod(int, int); | |
int main() | |
{ | |
int c, i, l; | |
char line[MAXLINE]; | |
l = 0; | |
while((c = getline(line, MAXLINE)) != 0){ | |
for(i = 0; i < c; ++i){ | |
++l; | |
if(l == FOLD){ | |
while(line[i] != ' ') | |
i--; | |
line[i] = '\n'; | |
l = 0; | |
} | |
} | |
printf("%s", line); | |
} | |
} | |
int getline(char s[], int lim) | |
{ | |
int c, i; | |
for(i = 0; i < lim-1 && (c = getchar()) != EOF && c !='\n'; ++i) | |
s[i] = c; | |
if(c == '\n') { | |
s[i] = c; | |
++i; | |
} | |
s[i] = '\0'; | |
return i; | |
} | |
int mod(int dividend, int divisor) | |
{ | |
return dividend - (dividend/divisor * divisor); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment