Wrap up Text

F

FUGATO

I need to help to wrap up text. I have a line all the text and I need
to wrap the text with 40 characters each line but without using
function. Somebody help me about that and give me an idea
 
B

Ben Pfaff

FUGATO said:
I need to help to wrap up text. I have a line all the text and I need
to wrap the text with 40 characters each line but without using
function. Somebody help me about that and give me an idea

Perhaps you could state your constraint more clearly. C code
requires using functions, so it's not possible in the form you
state.
 
K

Keith Thompson

FUGATO said:
I need to help to wrap up text. I have a line all the text and I need
to wrap the text with 40 characters each line but without using
function. Somebody help me about that and give me an idea

What exactly do you mean by "without using function"? Do you mean you
can't call any library functions, or that you can't define your own
functions, or something else?

Arbitrary restrictions like these usually mean that it's a homework
problem. If so, don't expect us to do the work for you. Try it
yourself and post some code if you're having problems.
 
C

CBFalconer

FUGATO said:
I need to help to wrap up text. I have a line all the text and I
need to wrap the text with 40 characters each line but without
using function. Somebody help me about that and give me an idea

No problem. Just compile the following:

/* ----- justify.c ----- Filter text file, right justifying by
inserting spaces between words. Words are anything separated by
blanks, tabs, newlines, formfeeds, bell, etc.

The single (optional) parameter is the output line length, and
defaults to 65. Execution without any input redirections causes a
help message.

This is a quick and dirty utility. Released to public domain by:
<mailto:[email protected]> */

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

#define RHDEFAULT 65
#define RHMIN 20

static int rhcol; /* right hand column limit */ static int
ragged; /* No rh justification, 0 init */

/* ------------------- */

/* This is very likely to be non-portable */ /* DOES NOT check fp
open for reading */ /* NULL fp is considered a keyboard here! */
static int akeyboard(FILE *fp) {
#ifndef __TURBOC__
# ifdef __STDC__
/* This dirty operation allows gcc -ansi -pedantic */
extern int fileno(FILE *fp); extern int isatty(int fn);
# endif
#endif
return ((fp != NULL) && isatty(fileno(fp))); } /* akeyboard */

/* ------------------- */

static void help(char *phrase1, char *phrase2) { if (phrase1)
fprintf(stderr, "%s", phrase1); if (phrase2) fprintf(stderr,
"%s", phrase2); fprintf(stderr, "\n" "Usage: justify
[rightmargin] <infile >outfile\n" " The default rightmargin is
65\n" " and values less than 20 are rejected\n" "\n" "A large
value of rightmargin will effectively\n" "convert all paragraphs
into single lines\n" "\n" "A negative rightmargin causes ragged
right\n" "\n" "A blank line delimits paragraphs\n"); } /* help */

/* ------------------- */

static int initialize(int argc, char *argv[]) { long rightcol;
char *err;

if (akeyboard(stdin) || (argc > 2)) { help(NULL, NULL); return 0;
} rhcol = RHDEFAULT; if (2 == argc) { rightcol = strtol(argv[1],
&err, 10); if (rightcol < 0) { rightcol = -rightcol; ragged = 1;
} if ((err == argv[1]) || (rightcol < RHMIN)) { help("Bad
argument: ", argv[1]); return 0; } else rhcol = rightcol; }
return 1; } /* initialize */

/* ------------------- */

static void cleanup(void) { } /* cleanup */

/* ------------------- */

/* ================================== */ /* Routines for text
input and output */ /* ================================== */

static void skipblanks(FILE *f) { int ch;

while ( (' ' == (ch = getc(f))) || ('\t' == ch) || ('\v' == ch)
|| ('\f' == ch) || ('\a' == ch) ) continue; ungetc(ch, f); } /*
skipblanks */

/* ------------------- */

/* The file is assumed to hold no control chars */ /* other than
\n \t \v \a and \f. A blank line */ /* marks a paragraph ending
word */ static int nextword(FILE *f, char *buffer, int max) { int
i, ch;

skipblanks(f); if (EOF == (ch = getc(f))) return 0;

/* Detect paragraph endings as \n\n */ if ('\n' == ch) {
skipblanks(f); ch = getc(f); if ('\n' == ch) { /* paragraph
ending */ buffer[0] = buffer[1] = ch; /* wd = "\n\n" */ buffer[2]
= '\0'; /* now we have to absorb any more blank lines */ do {
skipblanks(f); ch = getc(f); } while ('\n' == ch); ungetc(ch, f);
return 1; } } /* now ch holds the first non-blank. Use all
printable */ if (EOF == ch) return 0; if (!isgraph(ch)) {
fprintf(stderr, "'%c', 0x%x WARN: Invalid character\n", ch,
(unsigned)ch); }

i = 0; do { buffer[i++] = ch; if (i >= max) { /* truncate over
long words */ i--; break; /* leaving ch for next word */ } ch =
getc(f); } while (isgraph(ch));

ungetc(ch, f); /* save for next word, may be \n */ buffer =
'\0'; /* terminate string */ return 1; } /* nextword */

/* ------------------- */

static void justify(char *ln, int wdgaps, int xtra, FILE *out) {
int insert, i; static int oddln = 0; /* for rt left blank
insertion */ char ch;

#ifdef DEBUG
fprintf(out, "%2d %2d ", wdgaps, xtra);
#endif
insert = 0; oddln = !oddln; if (wdgaps) while (xtra > wdgaps) {
insert++; xtra -= wdgaps; } while ((ch = *ln++)) { putc(ch, out);
if (' ' == ch) { if (xtra) { xtra--; putc(' ', out); } for (i =
insert; i; i--) putc(' ', out); } } putc('\n', out); } /* justify
*/

/* ------------------- */

static int filter(FILE *in, FILE *out) { char *buf; char *ln; int
wdcount, lnlgh, wdlgh; char *eop = "\n\n"; /* end of paragraph */
int done, endpar;

if (!(buf = malloc(rhcol+1))) exit(EXIT_FAILURE); if (!(ln =
malloc(rhcol+1))) exit(EXIT_FAILURE);

done = !nextword(in, buf, rhcol + 1); endpar = !strcmp(buf, eop);

while (!endpar && !done) { /* form paragraph */ wdlgh =
strlen(buf); wdcount = 0; *ln = '\0'; lnlgh = 0;

while ((((lnlgh + wdlgh) < rhcol) || !lnlgh) && !done && !endpar)
{ /* form a line */ if (lnlgh) ln[lnlgh++] = ' '; strcpy(ln +
lnlgh, buf); lnlgh += wdlgh; wdcount++;

done = !nextword(in, buf, rhcol + 1); endpar = !strcmp(buf, eop);
wdlgh = strlen(buf); }

/* dump the line, wdcount words */ if (endpar || done) lnlgh =
rhcol; if (ragged) fprintf(out, "%s\n", ln); else justify(ln,
wdcount-1, rhcol-lnlgh, out);

if (endpar) { fputc('\n', out); done = !nextword(in, buf, rhcol +
1); endpar = !strcmp(buf, eop); } } return 0; } /* filter */

/* ------------------- */

int main(int argc, char *argv[]) { if (!initialize(argc, argv))
return EXIT_FAILURE; else { (void)filter(stdin, stdout);
cleanup(); } return 0; } /* main */

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,175
Messages
2,570,947
Members
47,498
Latest member
yelene6679

Latest Threads

Top