Skip to content

Instantly share code, notes, and snippets.

@Learath2
Last active August 29, 2015 14:04
Show Gist options
  • Save Learath2/ffea45fe6de3c9bcfb7d to your computer and use it in GitHub Desktop.
Save Learath2/ffea45fe6de3c9bcfb7d to your computer and use it in GitHub Desktop.
K&R2 Exercise 1-22
/*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