Thread: Formatting using arrays

  1. #1
    Registered User Alienchicken's Avatar
    Join Date
    Feb 2009
    Posts
    22

    Formatting using arrays

    Ok I have a file called paragrapgh.txt, in which i store into an array, now i ask the user to enter W, the maximum number of character allowed per line.

    Now i need a function to start displaying this on the screen separated by 1 space. If the word has o be displayed but not enough characters remain go to a new line.



    I am having great difficulty in implementing code to simulate the aligment

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    OK, first thing, post up your code, and highlight it, and click on the #hash char at the top of the reply to thread window.

    That puts code tags around, so it looks like code, on the forum.

    Then give us a little of the input and the ideal output you need. Your description is only going to go so far - and details are very important.

    And welcome to the forum!

  3. #3
    Registered User Alienchicken's Avatar
    Join Date
    Feb 2009
    Posts
    22

    Unhappy

    Thanks for the welcome my friend
    ok here's what i have

    insert
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #define Max 1000
    
    main (){
     
         
    FILE * in=fopen("paragraph.txt","r");     
    
    int choice,n=1,w;
    char ch;
    char str[Max];
    int i,j,a,b,len,rem;
    
    if (in==NULL){
                  printf("Cannot Locate File\n");
                  exit (1);
                  
                  }
                  
         while ((ch=getc(in))!=EOF){
                 str[n++] = ch;
                 }         
          // for(i=0;i<n;i++){
                   //   printf("%c",str[i]);       
                   //   }
                printf("Please enter your desired lenght of a line:");
                   scanf("%d", & w); 
                   
           len=0;
           rem=w;        
         for (j=1;j<=n;j++){
                                  printf("%c",str[j]);
                                  if (j%w==0){
                                                       
                                              printf("\n");
                                              }
                                  len++;
                                  rem=rem-len;
                                  }
                                                                                       
                                                         
           system ("pause");
           return 0;
           }                                   
    
                                              }

    i'm trying to format it so that if there isn't enough space for a word go to a new line. Cna't quite code that part

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    you need to put the & right next to the w, on that scanf() call for the width

    j should being your printing at 0, not 1

    j < n, instead of j <= n.

    if(j % w == 0 && j), instead of j % w == 0

    Now probably the biggest help of all. Get your coding style as clear and logical as you can:

    Code:
           len=0;
           rem=w;        
         for (j=1;j<=n;j++){
                                  printf("%c",str[j]);
                                  if (j%w==0){
                                                       
                                              printf("\n");
                                              }
                                  len++;
                                  rem=rem-len;
                                  }
    
    
         len=0;
         rem=w;        
    
         for (j=1;j<=n;j++) {
            printf("%c",str[j]);
            if (j%w==0) {
               printf("\n");
            }
             len++;
             rem=rem-len;
         }
    Your eye will become trained to catch all kinds of bugs if you'll let it, just by lining up your closing curled brace, with the for or if or while that starts that block of code.

    Curled braces are too important to put right next to another char - let 'em breathe!

    Now, you're not wasting screen space, and your eye will start training itself, AND others who look at your code, can use their own trained eye to scan it, and troubleshoot it, much easier and quicker.
    Last edited by Adak; 02-08-2009 at 07:50 PM.

  5. #5
    Registered User Alienchicken's Avatar
    Join Date
    Feb 2009
    Posts
    22
    The input is a follows;

    Please enter the number of chars per line: 26

    Current output:
    Type the answers to the fo
    llowing questions into a s
    ingle file.

    Desire output:
    The answers to the
    following questions into a
    single file

  6. #6
    Registered User Alienchicken's Avatar
    Join Date
    Feb 2009
    Posts
    22
    Yep i fixed that

  7. #7
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    You don't want a newline to break a word? Perhaps use an array of size W that temporarily stores the line before printing to stdout.

  8. #8
    Registered User Alienchicken's Avatar
    Join Date
    Feb 2009
    Posts
    22
    Quote Originally Posted by itCbitC View Post
    You don't want a newline to break a word? Perhaps use an array of size W that temporarily stores the line before printing to stdout.
    yeah i tried but i can't quite seem to traverse the array to print the desired output

  9. #9
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Can you post that code?

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    AC, you can't do what you want, on a char by char basis. You have to know the size of the word that will be printed next, and see if it's larger than the remaining spaces you have in that width of the line.

    lenOfWord = strlen(word);

    something like that, before you start printing each word, is the way to go.

  11. #11
    Registered User Alienchicken's Avatar
    Join Date
    Feb 2009
    Posts
    22
    OKay this is just a rough draft, (forgive me if i made any syntax errors so far)
    Still i can't seem to execute this one as well.
    Code:
          while (j!=n){
                 printf ("\n");
          while (i!=w){
                
                while(str[j]!=' '){
                                word[i]=str[i];
                                len=j++;
                                j++;
                                i++;
                                }
                                
                  if (str[j]==' '){
                                word[i]=str[i];
                                len=j++;
                                j++;
                                i++;
                                }
                    rem_spaces=  rem_spaces-len;
                    
                    }
                     
                     if(word[i]<rem_spaces){
                                            arr[i]= word[i];
                                            }
                                   else{
                                        j=j-rem_spaces; 
                                        }
                        printf("%c", arr[i]);
                                    }

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    If you have the length of the word (something wrong with strlen()? ), and you have enough space for the length of the word, then you're good to print it.

    No last if(word[i] < rem_spaces...etc., is necessary (or desirable). If rem_spaces > 0, print it.

    This is first blush on this. I haven't tested any code for it.

  13. #13
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by Adak View Post
    If you have the length of the word (something wrong with strlen()? ), .
    word is never nul-terinated (in the code shown) - so strlen could not be used
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 04:24 PM
  2. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  3. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 10:47 PM
  4. dos game help
    By kwm32 in forum Game Programming
    Replies: 7
    Last Post: 03-28-2004, 06:28 PM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 09:24 PM