hi... Can any one help me about text justification("justification on both sides", or "double justification", where text lines are justified both on the left and on the right. That is, all lines except (usually) the last one have equal width; the method used to achieve that is typically to leave extra space between words as needed.).

Rules :

  1. Each line should start and end with an alphanumeric letter unless there is only one word in the line.
  2. Each line length should be equal to the user specified length.
  3. A word shouldn’t be broken into two parts.
  4. The white space should be distributed in a balanced way.

for example:

#include <stdio.h>
#include <ctype.h>
#include <string.h>

void main() {
       char sstr[] = " any text abouth 900 characters";

    int twid; 
    int slen = 0;

    slen = strlen(sstr); // Length of the input string
    //printf("\nlen :%d\n", slen);

    printf("Enter text width :");
    scanf("%d",&twid);
    //printf("\nText Width %d",twid);

    //I need help!!!

}

Edited 3 Years Ago by pyTony: fixed formating

I don't know much more about strings, even C! Some idea please...

first find out how many words are on the line, count the length of all the words. subtract that from the total line length and you get the number of spaces that you have to distribute between the words. divide that number by the number of words and you get the number of spaces you have to put between each word.

Example
"any text abouth 900 characters"
That contains 27 characters, not counting the spaces. If you want a line length of 40, you get (40 - 27) = 13 spaces that have to be distributed between the 5 words. Because there are no spaces after the last word, the 13 spaces have to be distributed over 4 areas, or 3 spaces between each word with 4 spaces between the last two words. That makes the final string look like this:

"any   text   abouth   900    characters"

thanks... it can be helpful.I think my brain doesn't work today. But think about very large strings. for ex: I have A string with 500 characters and I want to write it as piece of lines which contains 60 characters. How should I sperate?

use a data file that contains lots of words -- for example (USA) President's Lincoln's Gettysburge Address (you can use google to find it) or some other file you may already have on your computer. Read the file one word at a time, keep track of the total number of characters read (plus 1 for at least one space between words). When that sum >= 80 (or whatever number you choose) you know the last word read will not fint on the current line, so output all words and spaces read so far. You will have to use an array to hold the words that have been previously read.

Before you output the words, sum total length of the characters and substract from total desired line length. For example, if sum = 70 then 80 - 70 = 10. You will have to distribute 10 spaces between the words. If there are 6 words, then there will be 10/6 = 1 space between words, plus 4 spaces you will have to distribute between the first 4 words. So the first 4 words will have 2 spaces between them and the last 2 words will have only 1 space.

Once the number of spaces between words is derived the function can start outputting the line. start a loop

could u please be more specific? I have no time..ç F1!F1!F1!

buralarda bizim ruh yok. kimse hazir odev vermiyor, hepsi akil hocasi. Zaten hazir odev aramiyoruz. Yani koca web de boyle bir odevi yapan ilk ogranci gurubuyuz herhalde- ya da arastiran. hani tüm dünya yararlansin.

#include <stdio.h>
#include <ctype.h>
#include <string.h>

void justifytext ( char *mytext, int width);
void justifyline ( char *line, int linewidth );

void main() {
	
	

	char sstr[] = "The academic staff of the Faculty of engineering at " 
		"Fatih University have been spending efforts to maintain a high "
		"level of education and research at the faculty. As a result of "
		"these efforts, 14 laboratories were set up as of the year 2002. "
		"Science and technology have been advancing at a rapid pace, and "
		"information and technologies that are new at this time become "
		"outdated shortly. The Faculty sets sights at teaching modern "
		"engineering techniques to students, as well as providing them" 
		"with the means that would help them harmonize with future "
		"technologies. The Faculty also supports research and development "
		"activities. The Faculty's most important feature is the training "
		"of modern engineers equipped with various scientific thinking and "
		"analytical skills, and with the ability to put theory into practice "
		"in the best way.";

	int twid; 
	int slen = 0;
	
    slen = strlen(sstr); // Length of the input string
	printf("\nLength of your text : %d\n", slen);

    printf("Enter text width :");
	scanf("%d",&twid);
    printf("\nText Width %d\n",twid);

	justifytext ( sstr, twid );
	printf ("\n\n");
}

void justifytext ( char *mytext, int width){
	
	char temp[100];//a string array for calling my second function
	int i,j, currenti, counter = 0;

	for ( i = 0; isspace(mytext[i]); i++ );        //if there is any space at the 
	memmove ( mytext, &mytext[i], strlen(mytext) );//beginning of the text, DELETED.
	
	i = 0;	
	while (  counter < (int)strlen(mytext) ){//I choose such a control-loop, because I try
											 //some other ways and this is most trustable.
		
		for ( i, j = 0; j < width; i++, j++ ){//finding each parts of text to justify
			temp[j] = mytext[i];
		}		
	
		for ( i ; !(isspace(mytext[i])); i--, j-- );//I cannot sperate any word, looking for spaces
		currenti = i;                               //I will use it to start the next line
	
		for ( i; isspace(mytext[i]); i--, j-- );//This makes part of array suitable for my second function.
		counter = i; //for controlling loop      -my second function works for only strings that do not have
											   //-spaces neither at the begining nor at the end.

		for ( ++j ; j < 100; j++ ) // only temp[++j] = '\0' is enough without for loop
			temp[j] = '\0';        // but this makes me relax

		justifyline ( temp, width );

		i = currenti + 1;    // this is where I start to sperate next time
		
	}
}

void justifyline ( char *line, int linewidth )
{
	char *temp;   // it for usage of strtok function
	int i, j, spaces = 0, spacestoshare=0, equalshare=0, extras=0;

	for ( i = 0; line[i] != '\0'; i++ ){  //counting spaces. Also this means likely how many words there

		if ( isspace ( line[i] ) ){
			spaces++;
		}
	}

	spacestoshare = linewidth - strlen(line); 

	if ( spaces == 0 ){
		equalshare = spacestoshare+spaces; //if spaces zero, there is only a word means
		extras = 0;                        // I put all my spaces after the word. No extras
	}

	else {
		equalshare = (spacestoshare+spaces)/spaces;//How many spaces I will put between each word in equal
		extras = spacestoshare % spaces;           //Extra spaces to put
	}
	
	for ( temp = strtok ( line, " " ); temp != NULL; temp = strtok ( NULL, " ") ){ //takes each word sperately

		printf ("%s", temp); //first write one word

		for ( j = 0; j < equalshare; j++ ) { // add spaces after the word
			putchar (' ');
		}
		
		if ( extras != 0 ){ // if there is extra 
			putchar (' ');  //also add them
			extras --;
		}
	
	}
	putchar ('\n');	
}

here is what I want to do... But still have some problems. My while loop doesn't work exactly for all widths... It doesn't stop.

This article has been dead for over six months. Start a new discussion instead.