Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that asks the user to input a string and then prints this string

ID: 3601764 • Letter: W

Question

Write a program that asks the user to input a string and then prints this string out in 70 characters according to the above justification rules. You may assume that the user always inputs the line that is less than 70 characters. Below is an example dialog:

To randomly insert spaces, consider the following approach

Text justification is spacing of the text block so that both right and left side of the text have a straight edge. This is accomplished by adding spaces so that every line is exactly the same length. You are to implement simplified justification rules as follows. Every line should be exactly 70 characters long (keep this value in a named constant). To get to this line length, extra space is first added after punctuation marks. You need to consider only these marks: .,!?; That is, every punctuation mark may be followed by two spaces. If the spaces after punctuation marks are added and the line is still less than 70 characters, additional spaces are added after random words in the line. There is no need to ensure that the extra spaces are spread uniformly. Randomization is sufficient. A line that is shorter than 40 characters is considered the end of a paragraph is not justified. Hyphenation (breaking up of words) between lines is not required.

Need a c++ code. I don't know how to start it to be honest and my lab instructor wasn't helpful at all. Comments would be helpful too

Explanation / Answer

#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <cstdlib>
#include <ctime>
using namespace std;

void splitText( string text, vector<string> &words ) // Splits a string into words
{
string oneWord;
stringstream ss( text );
while( ss >> oneWord ) words.push_back( oneWord );
}

string combineText( vector<string> words ) // Concatenates words to a string
{
string text = "";
for ( int i = 0; i < words.size(); i++ ) text = text + words[i];
return text;
}

bool isPunctuation( string s ) // Ends in punctuation?
{
string punc = ",.;:!?";
return ( punc.find( s[s.size()-1] ) != string::npos );
}

string justified( string message, int linelength ) // Justifies a supplied message
{
vector<string> words;
int numWords;
int i, w;
int tempLength = 0, remain;

splitText( message, words ); // Split into words
numWords = words.size();   

for ( i = 0; i < numWords - 1; i++ ) // Except last word on line ...
{
if ( isPunctuation( words[i] ) ) words[i] = words[i] + " "; // Add blank for any punctuation
words[i] = words[i] + " "; // Add normal blank after a word
}

for ( i = 0; i < words.size(); i++ ) tempLength += words[i].size(); // Find current length and number of extra blanks needed
remain = linelength - tempLength;

srand( time(NULL) );
if ( remain >= 0 ) // Add blanks to remain random words
for ( i = 1; i <= remain; i++ )
{
w = rand() % ( numWords - 1 );
words[w] = words[w] + " ";
}
else
cout << "Too many characters in the string" << endl;

return combineText( words ); // Recombine
}


int main()
{
string message = "Contrary to popular belief, Lorem Ipsum is not simply random text.";
cout << "Original: " + message << endl;
cout << "Justified: " + justified( message, 70 ) << endl;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote