I am trying to make a function that trims a body of text to a max of 80 characters (for console printing) per line. If the line is > 80 characters it should change the last three characters to ... and put a new line character. Here is the code I have for the function:
Your code doesn't work for me. This function has to handle text with lines that can be more, less or exactly 80 characters. Only if it is >80 should it do any replacing.
If it is more than 80 characters, after it finds the 80th character shouldn't it skip the rest of the line by moving i to the start of the next line? Here is my attempt at showing what I mean with code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
char* console_format(char* string)
{
int i = 0, j = 0;
for (; i < strlen(string); i++, j++)
{
if (string[i] == '\n' || j == 80) //Shouldn't this be or?
{
string[i] = '\n';
string[i - 1] = '.';
string[i - 2] = '.';
string[i - 3] = '.';
for (; string[i] != '\n'; i++) //Go to end of line
continue;
j = 0;
}
}
return string; //I want the string to be returned for use with cout
}
This code here just crashes when I try to run it. It compiles okay though.
With your code it runs fine, but doesn't make any change to the input. It just returns the same string I passed. This was after changing the return type and the return statement.
Then use std::string::find() to find the first \n. If it is more than 80 characters
out, then copy just the first 80 characters, append the ellipsis, and repeat.
Note that find() has a parameter that allows you to tell it where to start searching.
Obviously you'd do the second search beginning where the last one left off.