Advertisement
vishnum

Pointers

Apr 28th, 2014
297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.44 KB | None | 0 0
  1. // Lab 10 ExTalker Skeleton Code
  2.  
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <ctype.h>
  6. #include <stdlib.h>
  7. #include <ncurses/ncurses.h>
  8.  
  9.  
  10. #define MAXWORDS 100
  11. #define WORDLEN 11
  12. #define DEBUG 0   // set to 0 to disable debug output
  13. int readWords(char *wl[MAXWORDS], char* filename); // reads words from the file
  14. // into wl and trims the whitespace off of the end
  15.  
  16. //modifies s to trim white space off the right side
  17. void trimws(char* s) ;
  18. void printWords(char* words, int wordCount, int rows, int columns);
  19.  
  20. int main(int argc, char* argv[]) {
  21.     char* wordlist[MAXWORDS];
  22.     int wordCount;
  23.     int i,j,k=0;
  24.     int columns=5;
  25.     int rows;
  26.     wordCount = readWords(wordlist, argv[1]);
  27.  
  28.     if (DEBUG) {
  29.         printf("Read %d words from %s \n",wordCount, argv[1]);
  30.         for (i = 0; i< wordCount; i++) {
  31.             printf("%s,", wordlist[i]);
  32.  
  33.         }
  34.         printf("\n\n");
  35.     }
  36.    
  37.    
  38. // most of your code goes here.
  39.    
  40.     if ((((double)wordCount)/columns)>((double)(wordCount/columns))){
  41.         rows=(wordCount/5)+1;
  42.     }
  43.     else{
  44.         rows=(wordCount/5);
  45.     }
  46.     printf("%d\n\n", rows);
  47.    
  48.     char* words[rows][columns];
  49.     //char arrow[rows][columns];
  50.    
  51.     //Converts the list of words to a 2D array table
  52.     for(i=0; i<rows; i++){
  53.         for(j=0; j<columns; j++){
  54.             if (k<=MAXWORDS){
  55.                 words[i][j]=wordlist[k];
  56.                 k++;
  57.             }
  58.            
  59.         }
  60.     }
  61.    
  62.     for(i=0; i<rows; i++){
  63.         for(j=0; j<columns; j++){
  64.             if (k<=wordCount){
  65.                 printf("%15s", words[i][j]);
  66.                 k++;
  67.             }
  68.         }
  69.         printf("\n");
  70.     }
  71.    
  72.     // initscr();
  73.     // refresh();
  74.    
  75.     // printWords(words, wordCount, rows, columns);
  76.     // refresh();
  77.    
  78.     // sleep(5);
  79.     // endwin();
  80.    
  81.     //printf("\n%s", wordlist[0]);
  82.    
  83.  
  84. }
  85.    
  86.    
  87. // void printWords(char* words, int wordCount, int rows, int columns){
  88.     // int i, j, k;
  89.     // for(i=0; i<rows; i++){
  90.         // for(j=0; j<columns; j++){
  91.             // if (k<=wordCount){
  92.                 // printw("%15s", words[i][j]);
  93.                 // k++;
  94.             // }
  95.         // }
  96.         // printw("\n");
  97.     // }
  98. // }   
  99.    
  100. void trimws(char* s) {
  101.     int len = strlen(s) ;
  102.     int x;
  103.     if (len ==0) return;
  104.     x = len-1;
  105.     while (isspace(s[x]) && (x>=0)) {
  106.         s[x] = '\0';
  107.         x--;
  108.     }
  109. }
  110.  
  111. int readWords(char* wl[MAXWORDS], char* filename) {
  112.     int numread =0;
  113.     char line[WORDLEN];
  114.     char *p;
  115.     FILE* fp = fopen(filename,"r");
  116.     while (!feof(fp)) {
  117.         p  =fgets(line, WORDLEN, fp);
  118.         if (!feof(fp) && p !=NULL) {
  119.             trimws(line);
  120.             wl[numread] = (char *)  malloc(strlen(line)+1);
  121.             strcpy(wl[numread], line);
  122.             numread++;
  123.             }
  124.         }
  125.     fclose(fp);
  126.     return numread;
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement