Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Lab 10 ExTalker Skeleton Code
- #include <stdio.h>
- #include <string.h>
- #include <ctype.h>
- #include <stdlib.h>
- #include <ncurses/ncurses.h>
- #define MAXWORDS 100
- #define WORDLEN 11
- #define DEBUG 0 // set to 0 to disable debug output
- int readWords(char *wl[MAXWORDS], char* filename); // reads words from the file
- // into wl and trims the whitespace off of the end
- //modifies s to trim white space off the right side
- void trimws(char* s) ;
- void printWords(char* words, int wordCount, int rows, int columns);
- int main(int argc, char* argv[]) {
- char* wordlist[MAXWORDS];
- int wordCount;
- int i,j,k=0;
- int columns=5;
- int rows;
- wordCount = readWords(wordlist, argv[1]);
- if (DEBUG) {
- printf("Read %d words from %s \n",wordCount, argv[1]);
- for (i = 0; i< wordCount; i++) {
- printf("%s,", wordlist[i]);
- }
- printf("\n\n");
- }
- // most of your code goes here.
- if ((((double)wordCount)/columns)>((double)(wordCount/columns))){
- rows=(wordCount/5)+1;
- }
- else{
- rows=(wordCount/5);
- }
- printf("%d\n\n", rows);
- char* words[rows][columns];
- //char arrow[rows][columns];
- //Converts the list of words to a 2D array table
- for(i=0; i<rows; i++){
- for(j=0; j<columns; j++){
- if (k<=MAXWORDS){
- words[i][j]=wordlist[k];
- k++;
- }
- }
- }
- for(i=0; i<rows; i++){
- for(j=0; j<columns; j++){
- if (k<=wordCount){
- printf("%15s", words[i][j]);
- k++;
- }
- }
- printf("\n");
- }
- // initscr();
- // refresh();
- // printWords(words, wordCount, rows, columns);
- // refresh();
- // sleep(5);
- // endwin();
- //printf("\n%s", wordlist[0]);
- }
- // void printWords(char* words, int wordCount, int rows, int columns){
- // int i, j, k;
- // for(i=0; i<rows; i++){
- // for(j=0; j<columns; j++){
- // if (k<=wordCount){
- // printw("%15s", words[i][j]);
- // k++;
- // }
- // }
- // printw("\n");
- // }
- // }
- void trimws(char* s) {
- int len = strlen(s) ;
- int x;
- if (len ==0) return;
- x = len-1;
- while (isspace(s[x]) && (x>=0)) {
- s[x] = '\0';
- x--;
- }
- }
- int readWords(char* wl[MAXWORDS], char* filename) {
- int numread =0;
- char line[WORDLEN];
- char *p;
- FILE* fp = fopen(filename,"r");
- while (!feof(fp)) {
- p =fgets(line, WORDLEN, fp);
- if (!feof(fp) && p !=NULL) {
- trimws(line);
- wl[numread] = (char *) malloc(strlen(line)+1);
- strcpy(wl[numread], line);
- numread++;
- }
- }
- fclose(fp);
- return numread;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement