I'm trying to write a program that wraps words to the next line if there isn't room for them in the current column. I have a function I've written but it doesn't work (apparently strcmp() only works with consts ). This could is by no means perfect, in fact I'm sure it's terrible.
Example output:
Line read from file using fgets: "I like rice"
If max column width is 4 lets say, it should print out:
I
like
rice
Here's my current function: (does not work in the slightest)
Code:
void printWordsWrap(const char *line, int maxLineLength)
{
int i = 0;
int j = 0;
char *space = " ";
int counter = -1;
int startPosition = 0;
for (i = 0; i < maxLineLength; i++)
{
if (strcmp(space, line[i]) != 0) // ERROR: (line is underlined) says argument of type char is incompatble with const char*
{
counter++; // increment counter if it's not a space
}
if (strcmp(space, line[i]) == 0) // check to see if current char is a space, also, same error here
{
if (counter < maxLineLength) // checks to see if the current counter increment will fit in the column
{
for (j = startPosition; j < counter; j++) //prints the characters associated with how how the counter got
{
printf("%c", line[j]);
}
startPosition = counter; // sets startposition to where we left off, so we aren't starting at the start of the line everytime
}
else
{
printf("\n"); // throw a new line in there.
for (j = startPosition; j < counter; j++) // this assumes no words are longer then the column width
{
printf("%c", line[j]); // print the word
}
startPosition = counter;
}
}
}
return;
}