C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28695
Number of posts: 94715

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
i need a little help guys Posted by rookypooky on 16 Apr 2006 at 9:19 AM
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
Report
Re: i need a little help guys Posted by AsmGuru62 on 16 Apr 2006 at 10:12 AM
: 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
:
Not clear what you need... maybe, example?
Report
Re: i need a little help guys Posted by rookypooky on 16 Apr 2006 at 10:46 AM
: : 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
: :
: Not clear what you need... maybe, example?
:

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

for example:



#include <iostream>
#include <iomanip>

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.




Report
Re: i need a little help guys Posted by AsmGuru62 on 16 Apr 2006 at 1:31 PM
: : : 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
: : :
: : Not clear what you need... maybe, example?
: :
:
: well i need a code which justifies the text both sides in output.
:
: for example:
:
:
:
: #include <iostream>
: #include <iomanip>
:
: 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.
:
:
:
:
:
...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?


Report
Re: i need a little help guys Posted by rookypooky on 17 Apr 2006 at 2:18 AM
: : : : 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
: : : :
: : : Not clear what you need... maybe, example?
: : :
: :
: : well i need a code which justifies the text both sides in output.
: :
: : for example:
: :
: :
: :
: : #include <iostream>
: : #include <iomanip>
: :
: : 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.
: :
: :
: :
: :
: :
: ...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?
:
:


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

Report
Re: i need a little help guys Posted by AsmGuru62 on 17 Apr 2006 at 4:30 AM
This message was edited by AsmGuru62 at 2006-4-17 4:31:53

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:
// 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
}




Report
Re: i need a little help guys Posted by rookypooky on 19 Apr 2006 at 9:24 AM
: This message was edited by AsmGuru62 at 2006-4-17 4:31:53

: 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:
:
: // 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
: }
: 

:

:
:
:


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.


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

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("\n"); 

   ruler(scanSize,c); 

   printf("\n"); 

   return 0; 
} 

void ruler(int SIZE, char a[]) 
{ 
   int i=0,j, hold; 
    
   while(a[i]!='\0'){ 
      printf("%c",a[i]); 
    
       
      if((i+1)%SIZE==0){ 
         hold=i; 
          
         while((a[i]!=' '&&a[i+1]!=' ')){             
            i=i-1; 
         } 

         printf("\n"); 
         i=hold; 

      } 
          
       

      i++; 
   } 
}





 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.