i need a little help guys - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

i need a little help guys

rookypookyrookypooky Posts: 5Member
hello guys, could someone give me a a function for justifying text both sides in C?

thank you very much before hand

best ragards,
rookypooky

Comments

  • AsmGuru62AsmGuru62 Posts: 6,519Member
    : hello guys, could someone give me a a function for justifying text both sides in C?
    :
    : thank you very much before hand
    :
    : best ragards,
    : rookypooky
    :
    [blue]Not clear what you need... maybe, example?[/blue]
  • rookypookyrookypooky Posts: 5Member
    : : hello guys, could someone give me a a function for justifying text both sides in C?
    : :
    : : thank you very much before hand
    : :
    : : best ragards,
    : : rookypooky
    : :
    : [blue]Not clear what you need... maybe, example?[/blue]
    :

    well i need a code which justifies the text both sides in output.

    for example:



    #include
    #include

    using namespace std;

    int main()
    {
    const int max = 12;
    const int width = 6;
    for(int row = 1;row <= max;row++) {
    if(row % 2) {
    cout << setiosflags(ios::left);
    }
    else {
    cout << resetiosflags(ios::left);
    }
    for(int col = 1;col <= max;col++) {
    cout << setw(width) << row * col;
    }
    cout << endl;
    }
    return 0;
    }


    but i need for text and when user gives the wedth 30 or more, each line length should be equal to the user specified wedth. please give the function in C.

    thanks again before hand.




  • AsmGuru62AsmGuru62 Posts: 6,519Member
    : : : hello guys, could someone give me a a function for justifying text both sides in C?
    : : :
    : : : thank you very much before hand
    : : :
    : : : best ragards,
    : : : rookypooky
    : : :
    : : [blue]Not clear what you need... maybe, example?[/blue]
    : :
    :
    : well i need a code which justifies the text both sides in output.
    :
    : for example:
    :
    :
    :
    : #include
    : #include
    :
    : using namespace std;
    :
    : int main()
    : {
    : const int max = 12;
    : const int width = 6;
    : for(int row = 1;row <= max;row++) {
    : if(row % 2) {
    : cout << setiosflags(ios::left);
    : }
    : else {
    : cout << resetiosflags(ios::left);
    : }
    : for(int col = 1;col <= max;col++) {
    : cout << setw(width) << row * col;
    : }
    : cout << endl;
    : }
    : return 0;
    : }
    :
    :
    : but i need for text and when user gives the wedth 30 or more, each line length should be equal to the user specified wedth. please give the function in C.
    :
    : thanks again before hand.
    :
    :
    :
    :
    :
    [blue]...umm.. ok.. as a low level programmer I never use the iostream thing, so let me ask:

    Say, you have string "My Text".
    How will that string look when you justify it on width=24?

    [/blue]
  • rookypookyrookypooky Posts: 5Member
    : : : : hello guys, could someone give me a a function for justifying text both sides in C?
    : : : :
    : : : : thank you very much before hand
    : : : :
    : : : : best ragards,
    : : : : rookypooky
    : : : :
    : : : [blue]Not clear what you need... maybe, example?[/blue]
    : : :
    : :
    : : well i need a code which justifies the text both sides in output.
    : :
    : : for example:
    : :
    : :
    : :
    : : #include
    : : #include
    : :
    : : using namespace std;
    : :
    : : int main()
    : : {
    : : const int max = 12;
    : : const int width = 6;
    : : for(int row = 1;row <= max;row++) {
    : : if(row % 2) {
    : : cout << setiosflags(ios::left);
    : : }
    : : else {
    : : cout << resetiosflags(ios::left);
    : : }
    : : for(int col = 1;col <= max;col++) {
    : : cout << setw(width) << row * col;
    : : }
    : : cout << endl;
    : : }
    : : return 0;
    : : }
    : :
    : :
    : : but i need for text and when user gives the wedth 30 or more, each line length should be equal to the user specified wedth. please give the function in C.
    : :
    : : thanks again before hand.
    : :
    : :
    : :
    : :
    : :
    : [blue]...umm.. ok.. as a low level programmer I never use the iostream thing, so let me ask:
    :
    : Say, you have string "My Text".
    : How will that string look when you justify it on width=24?
    :
    : [/blue]

    yeah there will be string "Text" (pretty long text) and when we justify it on width=24 it will be:
    each line will have 24 characters including with spaces, and one thing is important the each line have to not break a long word into break.

    witing your repaly with example code

  • AsmGuru62AsmGuru62 Posts: 6,519Member
    [b][red]This message was edited by AsmGuru62 at 2006-4-17 4:31:53[/red][/b][hr]
    [blue]You need to build the list of words in that text. Then calculate the number of spaces between words:

    N spaces = (WIDTH - SUM OF ALL WORDS LENGTH) / (WORD COUNT - 1);

    Watch out for a single word text - that will give you divide by zero error, so you need to adjust the algorithm for that.

    After this you need to get the remainder of above expression:

    N add spaces = (WIDTH - SUM OF ALL WORDS LENGTH) % (WORD COUNT - 1);

    After all these preparations are done - you can justify text:
    [code]
    // pseudocode:
    while (more words left)
    {
    output next word
    if (that was last word)
    {
    break;
    }

    BLANKS = N spaces
    if (N add spaces != 0)
    {
    BLANKS++; N add spaces--;
    }
    output BLANKS number of spaces
    }
    [/code]
    [/blue]


  • rookypookyrookypooky Posts: 5Member
    : [b][red]This message was edited by AsmGuru62 at 2006-4-17 4:31:53[/red][/b][hr]
    : [blue]You need to build the list of words in that text. Then calculate the number of spaces between words:
    :
    : N spaces = (WIDTH - SUM OF ALL WORDS LENGTH) / (WORD COUNT - 1);
    :
    : Watch out for a single word text - that will give you divide by zero error, so you need to adjust the algorithm for that.
    :
    : After this you need to get the remainder of above expression:
    :
    : N add spaces = (WIDTH - SUM OF ALL WORDS LENGTH) % (WORD COUNT - 1);
    :
    : After all these preparations are done - you can justify text:
    : [code]
    : // pseudocode:
    : while (more words left)
    : {
    : output next word
    : if (that was last word)
    : {
    : break;
    : }
    :
    : BLANKS = N spaces
    : if (N add spaces != 0)
    : {
    : BLANKS++; N add spaces--;
    : }
    : output BLANKS number of spaces
    : }
    : [/code]
    : [/blue]
    :
    :
    :


    hello sir, please compile this code the watch the output, the programs seems working, but their is a big problem, the program devides the word. the program should not devide it, please take a look at the programm and correct it, i really need some help on this, cuz this programm really makes me crazy. and look at this photos:

    http://img381.imageshack.us/my.php?image=output16km.jpg

    http://img394.imageshack.us/my.php?image=output26kl.jpg


    the program's output should be like in that photos, hope for you help dear guys.


    [CODE]
    #include
    #include
    #include

    void ruler(int SIZE, char a[]);

    int main()
    {
    char c[]="We cant process trade into life without advertising. Advertisements are the most important things for trade. When we want to sell a product with a great benefit, we introduce our product to the customers by using the words such as 'the best, the dazzled, the impressive', and we use advertisements to effect the customers.";
    int scanSize;

    printf("Enter a text size: ");
    scanf("%d",&scanSize);

    printf("
    ");

    ruler(scanSize,c);

    printf("
    ");

    return 0;
    }

    void ruler(int SIZE, char a[])
    {
    int i=0,j, hold;

    while(a[i]!=''){
    printf("%c",a[i]);


    if((i+1)%SIZE==0){
    hold=i;

    while((a[i]!=' '&&a[i+1]!=' ')){
    i=i-1;
    }

    printf("
    ");
    i=hold;

    }



    i++;
    }
    }[/CODE]

Sign In or Register to comment.