CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Nov 2013
    Posts
    6

    Question Text Justification

    I need some guidance in this code,how can i start off with. As there is not much material on internet about this type of code so i need some help from you guys. thanks.

    Code:
                         <<Header file>
    
    
    #include <iostream>
    #include <string>
    #include <vector>
    #include "ctype.h"
    
    using namespace std;
    
    // some useful type definitions
    typedef vector<string> PARA;
    typedef unsigned int ERROR;
    
    #define MIN_COL_WIDTH 20
    #define MAX_COL_WIDTH 60
    
    // error codes
    #define ERROR_NONE 0
    #define ERROR_NO_CHARS 1
    #define ERROR_ILLEGAL_WIDTH 2
    
    // function prototype
    ERROR justify(unsigned int width, string in, PARA *out);
    // width is the desired width of line in output (justified) text.
    // "in" is the input string.
    // PARA is a vector of strings, where each element is one line
    // in the output justified text.
    // The first element of the PARA vector is the first line of the 
    // output text, the second element is the second line, and so on.
    // note that output text variable is declared in main function.
    // it is passed by reference to "justify".
    // The function returns one of the above error codes.
    
                             <<Main file>>
    #include "justify.h"
    
    int main() {
    	ERROR result;
    	unsigned int width;
    	string input;
    	PARA output;
    
    	while (1) {
    		// first clear the output string
    		output.clear();
    		cout << endl<< "Welcome to text justification!" << endl << endl;
    		cout << "Enter the output line width (between " << MIN_COL_WIDTH << " and " << MAX_COL_WIDTH << ", or 0 to exit): ";
    		cin >> width;
    		if (width == 0)
    			break;
    		cout << "Enter the string to justify below:" << endl << endl;
    
    		// read in the input string 
    		cin.ignore();
    
    		getline(cin, input);
    
    		// justify the input for given column width
    		result = justify(width, input, &output);
    
    		switch(result) {
    			case ERROR_NONE: 
    				cout << endl << "The justified text is:" << endl << endl;
    				for (unsigned int i = 0; i < output.size(); i++)
    					cout << output[i] << endl;
    				break;
    			case ERROR_NO_CHARS:
    				cout << "ERROR: No characters in input para!" << endl;
    				break;
    			case ERROR_ILLEGAL_WIDTH:
    				cout << "ERROR: Column width out of range!" << endl;
    				break;
    			default:
    				cout << "ERROR: Internal error!" << endl;
    				break;
    		}
    	} 
    }
    
    
                             <<Implementation file>>
    
    #include "justify.h"
    
    // implement the justify function below
    // use push_back method of string class to insert a new character
    // at the end of a string
    // You can terminate a string by calling push_back on it with '\0'.
    // Use the clear method of string class to remove all the
    // characters from the string.
    
    ERROR justify(unsigned int width, string in, PARA *out) {
    
    	
    	//  make sure that column width is valid
    
    	// make sure that the input is not empty string
    
    	// justify the text!
    
    } // end function justify
    Last edited by VictorN; November 28th, 2013 at 02:22 AM. Reason: redundant code tags were removed

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Text Justification

    As this looks like an assignment, we won't provide you with the code. However, before you code the function, think about how you would do it just using pen & paper. Get some squared paper and mark on the text. Then alter the text placement so that it is justified. What steps did you take to do this? Now write down those steps as an algorithm for text justification. Then write a program design using the algorithm, then code the program from the design then finally test/debug the code.

    Hint. Look at the number of spaces before/during/after words and calculate how many you actually need to justify the text.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: Text Justification

    Quote Originally Posted by talhakhan797 View Post
    ... As there is not much material on internet about this type of code ...
    Really? I just tried googling for "monospace text justification algorithm" and got over 3 million hits!
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured