Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need to limit the amount of characters read into an array.

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Here is my code. ANY suggestions would be amazing. Basically my question as shown in the subject is that I need some way of taking the data and then stopping after a hundred characters have been read and then go to the next sentence.

#include <stdio.h>
#include <stdlib.h>
int main(void) {
    FILE *f;
    char filename[80];
    char ws;
    int k,r,ls;
    char word[100][100];
    char name[100];
    /*  get a filename from the user  */
    printf(&quot;Enter a filename to read.\n&quot;);
    scanf(&quot;%s&quot;, filename);
    /*  open a file for input  */
    if (( f = fopen(filename, &quot;r&quot;)) == NULL) {
        printf(&quot;Can't open %s.\n&quot;, filename);
        exit(1);
    }
    /*  read one character at a time until end-of-file  */
    for (k=0; k<100;++k){            /*This will fill the array*/
        for (r=0;r<100;++r){        /*This will fill the array*/
            word[k][r]=getc(f);        /*Writes the char into the array*/
            ws=word[k][r];            /*Set so ws will equal the array*/
            ls=ws;                
    
    if (ws=='\n'){                    /*checks for new line*/
        while (r<100){
            word[k][r]=' ';            /*this will fill the array if new line is present*/
            r=r+1;
                     }
            ++k;                    /*this will move the array*/
            r=-1;                    /*corrects the begining number for r*/
                 }
    
    if (ls==13){                    
        word[k][r]=' ';
               }
                
    if (ws=='.'){                    /*checks for end of sentence*/
        r=r+1;
        while (r<100){
            word[k][r]=' ';            /*fills loop*/
            r=r+1;
                      }
        ++k;
        r=-1;
                 }
        
    if (ws=='!'){                    /*same as end of sentence but for !*/
        r=r+1;
        while (r<100){
            word[k][r]=' ';
            r=r+1;
                      }
         ++k;
         r=-1;
                 }
     
     if (ws=='?'){                    /*same as end of sentence but for ?*/
        r=r+1;
        while (r<100){
            word[k][r]=' ';
            r=r+1;
                      }
        ++k;
        r=-1;
                  }
         
          }
      }
     
    for (k=0;k<100;++k){
        for (r=0; r<100;++r){
            printf(&quot;%c&quot;,word[r][k]);    /*THis will print out the columns instead of in rows*/
                                
                            }
        printf(&quot;\n&quot;);
                        }
                         
    fclose(f);
    return 0;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top