Hi, I'm new to the forums and just started learning the C programming language.
I tried making my own program that wraps paragraphs to a certain column width and changes "tabs" to a certain number of spaces, but although it seems like it should be working perfectly, it does not. The output makes a newline after the first letter, and then no more new lines are created. The Tab feature does work correctly though.
I've looked through the code many times, and I have no idea what could be wrong. Although it would probably be a pain in the ass to look at the code for the first time, I thought that I have nothing to lose by posting it.
It works by measuring the distance between space characters and seeing if it is over the column width. Here's the code:
Code:
#include <stdio.h>
#define TABSTOP 5
#define COLUMNWIDTH 20
#define MAXCHARACTERS 5000
#define SPACE 32
#define MAXSPACES 4000
#define MAXLINES 4000
main(){
/* Definition of Variables */
char characters[MAXCHARACTERS];
char spaces[MAXSPACES];
char enters[MAXLINES];
int i, c, j, f, p, q, t, z;
/* Gets characters & spaces and correlates each with a variable in an array, */
/* including transferring tabs to the write number of spaces */
p = 1;
spaces[p] = 0;
for (i=0; (c=getchar())!=EOF; ++i) {
if (c == '\t') {
i-=1;
for (j=0; j<=TABSTOP; ++j) {
characters[i] = SPACE;
++i;
}
} else {
characters[i] = c;
}
if (c == SPACE) {
++p;
spaces[p] = i;
}
}
if (c == EOF) {
++i;
characters[i] = '\0';
++p;
spaces[p] = i;
}
z = p;
putchar('\n');
/* Measures Distance between Spaces and finds newline points */
p = 1;
q = 2;
t = 1;
while (spaces[q]<=(z)) {
while ((spaces[q]-spaces[p]) <= COLUMNWIDTH) {
++q;
}
if ((spaces[q]-spaces[p]) > COLUMNWIDTH) {
enters[t] = spaces[q-1];
++t;
p = q-1;
}
}
/* Prints paragraph with newline points*/
t = 2;
for (i=0; characters[i]!='\0' && i<MAXCHARACTERS; ++i) {
putchar(characters[i]);
if (i == enters[t]) {
putchar('\n');
++t;
}
}
}
Any idea why it may not be working or any ideas how I could organize it better (through functions maybe?), or even criticism, is greatly appreciated.
Thanks,
Edan.