1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| #include <string>
#include <ctype.h>
void displayText(string text)
{
const unsigned CONSOLE_WIDTH(80);
unsigned width(0);
width = CONSOLE_WIDTH;
string buffer("");
char character('a');
char charincr('a');
while (text.length() >= CONSOLE_WIDTH)
{
for (int i = width; i > 0; i--) //read string backwards char by char starting from pos 80 (CONSOLE_WIDTH)
{
character = text[i];
if (isalpha(character) || ispunct(character))
{
charincr = text[(i+1)];
if(isalpha(charincr) || ispunct(charincr)) {} //next char is a letter -> inside a word -> coninue backwards (continue the loop)
else if (isspace(charincr)) //next char is a space -> the current char is the end of a word -> cut string and display line.
{
buffer = text.substr(0,(i+1));
cout << buffer << endl;
text.erase(0, (buffer.length()+1));
i=0; //force loop end
width = CONSOLE_WIDTH; // reset width
}
}
}
}
cout << text << endl;
} |
Partager