Click to See Complete Forum and Search --> : String editing in C++
dunno
02-04-2001, 10:21 AM
I need help. I've been trying to do this but I've been failing the last few days.
Can someone write a C++ function that will take a string, wrap it and indent it correctly and then return the editied string?
string wrapString(string a) { };
What I want it to do exactly is make sure that the lines of the string don't exceed 80 characters, and every line aside from the first will be indented 5 spaces.
For example, if this was the string... It would have no breaks in it or anything. It would just be a long line of text. I'm not sure how long this is, but this is what I want it to look like:
For example, if this was the string... It
would have no breaks in it or anything.
It would just be a long line of text.
I'm not sure how long this is, but this
is what I want it to look like:
TetsuoII
02-04-2001, 12:41 PM
Here's a quick and ugly version which is working somewhat. It needs a lot of work, but I think you'll get the general idea from this:
#include <iostream>
#include<string>
const int maxLength = 80; // maximum string length
const int tabLength = 8; // number of spaces that equals one tab
std::string wrapString(std::string original)
{
std::string resultString = "";
// if it's less than [maxLength] chars, we are safe
if( original.length() <= maxLength )
resultString = original;
int position = maxLength;
// We find the first part of the string
while( original[ position ] != ' ' && position >=0 )
--position;
// and copy it
resultString.append( original.substr( 0, position ) );
int lineLength = position + tabLength;
++position;
int startPosition;
while( ( position + ( lineLength - tabLength ) ) < original.length() )
{
startPosition = position;
resultString.append( "\n\t" ); // add a newline and a tab
position += lineLength - tabLength;
while( original[ position ] != ' ' )
--position;
++position;
std::cout << "startPosition: " << startPosition << " position: " << position << std::endl;
resultString.append( original.substr( startPosition, ( position - startPosition ) ) );
}
if( position < original.length() )
{
resultString.append( "\n\t" );
resultString.append( original.substr( position, original.length() - position ) );
}
return resultString;
}
int main( int argc, char* argv[] )
{
std::string theString("For example, if this was the string... It would have no breaks in it or anything. It would just be a long line of text. I'm not sure how long this is, but this is what I want it to look like:" );
std::cout << "The string before : " << theString << "\n\n\n";
std::string newString = wrapString( theString );
std::cout << "The string after :\n" << newString << std::endl;
return 0;
}
T.
Sterling
02-04-2001, 05:16 PM
Word wrapping gets ugly. Especially when you've got nonprinting characters in the string (like VT220 control codes or bel codes) or characters of variable width (tab characters). I don't think there's any generic wordwrap library, either. Too many different things to deal with, unfortunately.
dunno
02-05-2001, 05:36 PM
Well, here is what I have, it works under some cases, but it often doesn't work and i'm not sure why... If you don't mind please test this and tell me any problems you notice.
#include <string>
#include "fixLine.h"
// takes a line and wraps it at 75 chars, indented 5 spaces
string fixLine(string a)
{
// check to see if it needs work
if (a.length() <= 75)
{
return a;
}
// insert the first break
for(int i = 75; i > -1; i--)
{
// find the last space and insert "\n "
if(a[i] == ' ')
{
a.insert(i, "\n ");
break;
}
// if no space is found...
if(i == 0)
{
// insert the break anyway
a.insert(75, "\n ");
break;
}
} // loop until the substring is not long enough
for (int i = 1; a.substr(i * 75).length() > 75; i++)
{
// edit only the already edited
string temp = a.substr(i * 75);
for(int m = 75; m > 5; m--)
{
if (temp[m] == ' ')
{
temp.insert(m, "\n ");
break;
}
if (m == 6)
{
temp.insert(75, "\n ");
break;
}
}
// change a
string alreadyFixed = a.substr(0, i * 75);
alreadyFixed += temp;
a = alreadyFixed;
}
return a;
}
[ 05 February 2001: Message edited by: dunno ]
f'lar
02-05-2001, 07:45 PM
The 75 needs to be a 76 for the \0 if you are talking about a 5 space indentation.
One more thought: It sounds like you want it to return an array of strings, but you could also format the existing string to add \9 and \n characters so it is still one huge string, it will just output right.
[ 05 February 2001: Message edited by: f'lar ]