printing long text

This is a discussion on printing long text within the C Programming forums, part of the General Programming Boards category; I am trying to print a sting. I want 50 char a line and i don't want the word at ...

  1. #1
    Unregistered
    Guest

    printing long text

    I am trying to print a sting. I want 50 char a line and i don't want the word at the end of the line to break....

    I can't figure it out!!!


    Here is what I have so far

    int main(void)
    {
    const char text[]="This is a text I am trying to print this long\
    text without having to break a word.";

    int n;

    while (text!=EOF)
    {
    n=strlen(text);
    if (n>50)
    {
    n--;
    while (text[n]!=' ')
    {
    continue;
    }
    text[n]='\n';
    text[n+1]='\0';
    }
    printf("%s",text);
    }
    return 0;
    }

  2. #2
    Registered User moonwalker's Avatar
    Join Date
    Jul 2002
    Posts
    282

    hmm

    I already posted this in the "word wrap" thread

    here...

    if you're looking for wordwrap in console,
    look here.....

    Code:
    void mywordwrap(FILE *fp, char string[], short maxLineWidth) {
    
       short startIndex;
       short endIndex;
       short actualLineWidth;
    
       if (fp == NULL || string == NULL || maxLineWidth <= 0) {
          return;
       }
    
       
       startIndex = 0;
       endIndex = strlen(string);
    
    
        while (startIndex < endIndex) {
    
          
         while (startIndex < endIndex && isspace( string[startIndex] )) {
    	 ++startIndex;
          }
    
          
          actualLineWidth = maxLineWidth;
          if (endIndex - startIndex > maxLineWidth) {
             while (actualLineWidth > 0 &&
                !isspace( string[ startIndex+actualLineWidth-1 ]) &&
                !isspace( string[ startIndex+actualLineWidth   ]) ) {
                --actualLineWidth;
             }
    	}
    
    
           if (startIndex < endIndex && actualLineWidth > 0) {
    	 fprintf(fp,"%.*s\n",actualLineWidth, &string[ startIndex ]);
          }
    
          
          startIndex += actualLineWidth;
       }
    }
    //syntax mywordwrap(stdout, chararray, 70);

    put the above function above your main function,

    in main(), you have

    char text[]="This is a text I am trying to print this long
    text without having to break a word.";

    you said you wanted a 50 char line....
    so do this

    mywordwrap(stdout, text, 50)

    got it now ?


    GOOD LUCK

  3. #3
    Unregistered
    Guest
    thanks that is nice of you but I am a beginner C This is a homework problem....I need something much simpler

    I tried to use printf("%0.50s",text); it works but it prints only one line

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    There is no "simple" way to do this. The only way to do it is:

    Code:
    count the caracters in a word
    if the total characters + the word length + 1 is >= X
        print a new line
        total characters = 0
    print the word
    total character += word length + 1
    print a space
    Put that in a loop and loop until you run out of words.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Unregistered
    Guest
    quzah I hope you are still online

    I am having trouble writting the codes for your instructions...I need help

    This is what I have so far:

    int main(void)
    {
    const char text[]="This is a text I am trying to print this long\
    text without having to break a word.";

    //count the caracters in a word
    //if the total characters + the word length + 1 is >= X
    // print a new line
    // total characters = 0
    //print the word
    //total character += word length + 1
    //print a space


    int k;

    for (k=0;k<strlen(text);k++)
    {

    if (((strlen(text)) + (word length + 1))>=60)
    printf("\n");


    printf(" ");
    }
    return 0;
    }

  6. #6
    Registered User moonwalker's Avatar
    Join Date
    Jul 2002
    Posts
    282
    ok dude...
    here's a clue...

    1) Take a list of all the places in the sentence where
    there are spaces

    2) find the nearest space to 50 (if you want to allow 50 characters
    per line)

    3) use a for loop to print till that place

    4) loop it again for the whole sentence

    Code:
    	
                    int array[50]; //holds places where space character is in the given string
    
    	for (i = 0; i<strlen(text); i++)
    	{
    		if (text[i] == ' ')
    		{
    			array[k] = i+1;
                                                    printf("%d\n", array[k]);
                                                    k++;
    			
    		}
    	}
    The above code will give you wherever there are spaces in
    a given string.

    Try working the rest of it for yourself...

    hope this worked as a clue for you..

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    95
    something like this will probably work fine:
    Code:
    int spaceplace,counter=0,printcounter=0;
    char sentence;
    
    
    ....
    ....
    while(couner<strlen(sentence))
    {
      while(counter<25)
      {
        if(sentence[counter]=' ')
          spaceplace = counter;
        counter++;
      }
      for(printcounter=0;printcounter<spaceplace;printcounter++)
        printf("%c",sentence[printcounter]);
      printf("\n");
    
      counter=printcounter+1;
    }
    I think somethin like that will work, or atleast give you an idea

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 15
    Last Post: 09-30-2008, 03:12 AM
  2. sorting using pointer to pointer not working
    By eager2no in forum C Programming
    Replies: 17
    Last Post: 09-21-2008, 01:52 AM
  3. Replies: 3
    Last Post: 05-25-2005, 02:50 PM
  4. Insertion Sort Problem
    By silicon in forum C++ Programming
    Replies: 1
    Last Post: 05-08-2005, 01:30 PM
  5. Ok, Structs, I need help I am not familiar with them
    By incognito in forum C++ Programming
    Replies: 7
    Last Post: 06-29-2002, 10:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21