Linkedin Interview Question
Software Engineer / DevelopersIt says left-right justified, which I take to mean, 'left /and/ right justified'. If that's so, you're not right-justifying your output.
std::string inputString( "very very long long this is a very very long long string" );
std::string tempBuff;
std::stringstream inputStringStream( inputString );
unsigned int currentCharCount = 0;
unsigned int tempCharCount = 0;
while ( inputStringStream >> tempBuff )
{
tempCharCount = currentCharCount + tempBuff.size( );
if (tempCharCount >= MAX_CHAR_IN_LINE )
{
std::cout<<"\n";
//reset
currentCharCount = 0;
tempCharCount = 0;
}
std::cout<<tempBuff<<" ";
currentCharCount = currentCharCount + tempBuff.size( ) + 1;
}
/*
* given a long string str, print in line, no breaking of words, left-right, at most L characters per line
* str = ab cd ef, L = 2, output ab\ncd\nef\n
* str = ab d e hl, L = 3, output abd\nehl\n
* str = abc efg, L = 4, output abc\nefg\n
* str = a, L = 2, output a\n
*/
void format_print(const char* str, int L)
{
char buffer = new char[L];
int buffer_head = 0;
int buffer_size = 0;
for (; *str; str++)
{
if (buffer_size == L)
{
//output from buffer_head, for buffer_size
if (*str == ' ')
{
last_word_right_bound = buffer_size - 1;
}
for (int i = 0; i < buffer_size; i++)
{
printf("%c", buffer[(buffer_head + i - 1 + L) % L]);
if (i == last_word_right_bound)
{
for (int j = i + 1; j < buffer_size; j++)
{
printf(" ");
}
printf("\n");
}
}
buffer_head = buffer_size = 0;
}
{
buffer[(buffer_head + buffer_size - 1 + L) % L] = *str;
if (*str == ' ' && buffer_size > 0)
{
last_word_right_bound = buffer_size - 1;
}
buffer_size++;
}
}
if (buffer_size > 0)
{
bool only_space = true;
for (int i = 0; i < buffer_size; i++)
{
printf("%c", buffer[(buffer_head + i - 1 + L) % L]);
only_space &= (buffer[(buffer_head + i - 1 + L) == ' ');
}
for (; !only_space && i < L; i++)
{
printf(" ");
}
printf("\n");
}
}
For both left and right justification, first do usual step of trying to put the words in each line with single space. Then, if we end up with n spaces at end of line and the next word is larger than n-1, then we need to distribute the n spaces evenly across the line.
For this even distribution, if current spaces upto last word in the line = x, then do
intpart = n/x
remainderpart = n mod x.
Add intpart spaces to all spaces, then starting from first or last, add the reminderpart spaces until they are exhausted.
E.g., if there are 27 spaces at end, and next word is 30 characters, also suppose current line has 11 words or 10 spaces upto last word, then we need to distribute these 27 spaces over the 10 spaces. So 27/10 = 2 int part, 7 reminder part. So we ad 2 spaces to all 10 spaces, and then remaining 7 spaces, we add i space each to 7 spaces starting from beginning or end.
Another approach could be to do the following:
1. Have 2 pointers - begin and end.
2. Set begin to the start of the long text.
3. Advance end L units. Check to see if end is now a blank space. If not, move back end until a blank space is encountered.
4. Print all characters from begin to end. Print new line character.
5. Do end++; begin = end;
6. Repeat steps 1-6 until end hits the end of the long text.
- metacret August 13, 2011