training

Code I wrote during training
git clone git://git.bitsmanent.org/training
Log | Files | Refs | README

diffwordc.c (2379B)


      1 /* Exercise 8.34 (c) */
      2 
      3 /* NOTE: orderall() is not so "smart" */
      4 
      5 #include <stdio.h>
      6 #include <string.h>
      7 #include <stdlib.h>
      8 #include <ctype.h>
      9 
     10 #define SIZE 10
     11 #define BUF 254
     12 #define MAX 30
     13 
     14 int ison(char s[][BUF], char *word);
     15 char *sclean(char *s);
     16 void orderall(char s[][BUF], int n[MAX]);
     17 
     18 
     19 int main(void)
     20 {
     21    char string[SIZE][BUF] = { { 0 } }, *tokp;
     22    int i, idx, midx = 0, counter[MAX] = { 0 };
     23    char sstr[MAX][BUF] = { { 0 } };
     24 
     25    /* Take the strings */
     26    printf("Give me a string (%d to end): ", EOF);
     27    gets(string[i]);
     28 
     29    while( atoi(string[i++]) != EOF ) {
     30       printf("Give me a string (%d to end): ", EOF);
     31       gets(string[i]);
     32    }
     33 
     34    string[i - 1][0] = '\0'; /* Remove the '-1' */
     35 
     36    for(i = 0; i < SIZE; i++) {
     37 
     38       /* count the words */
     39       tokp = strtok(string[i], " ");
     40       while( tokp != NULL ) {
     41 
     42 	 if( (idx = ison(sstr, tokp)) > midx)
     43 	    midx = idx;
     44 
     45 	 if(idx == -1) {
     46 	    strcpy(sstr[++midx], tokp);
     47 	    counter[midx] = 1;
     48 	 }
     49 	 else {
     50 	    ++counter[idx];
     51 	 }
     52 
     53 	 tokp = strtok(NULL, " ");
     54       }
     55    }
     56 
     57    orderall(sstr, counter); /* Order the output */
     58 
     59    /* Show the results */
     60    printf("\nTABLE..\n\n%4s%31s\n", "Word", "Occurrences");
     61    printf("-----------------------------------\n");
     62    for(i = 0; i < MAX; i++) {
     63       if( strlen(sstr[i]) )
     64          printf("%c%-13s\t\t%11.2d\n",
     65 	    toupper((int)sstr[i][0]), &sstr[i][1], counter[i]);
     66    }
     67 
     68    printf("\n");
     69 
     70    return 0;
     71 } /* E0F main */
     72 
     73 /* locate word to s[][] strings */
     74 int ison(char s[][BUF], char *word)
     75 {
     76    int i;
     77 
     78    for(i = 0; i < MAX; i++) {
     79       if( ! strcmp(s[i], sclean(word)) )
     80 	 return i;
     81    }
     82 
     83    return -1;
     84 } /* eof ison() */
     85 
     86 /* clean a string :-) */
     87 char *sclean(char *s)
     88 {
     89    int i;
     90 
     91    for(i = 0; s[i] != '\0'; i++)
     92       s[i] = !ispunct((int)s[i]) ? tolower((int)s[i]) : ' ';
     93 
     94    return s;
     95    
     96 } /* eof sclean() */
     97 
     98 /* Order alphabetically s and swap the indexes in the n array */
     99 void orderall(char s[][BUF], int n[MAX])
    100 {
    101    int i, j, num;
    102    char string[BUF] = { 0 };
    103 
    104    for(i = 0; i < MAX - 1; i++) {
    105       for(j = 0; j < MAX - 1; j++) {
    106 
    107          if( strcmp(s[j], s[j + 1]) > 0 ) {
    108 
    109 	    /* swap the strings */
    110 	    strcpy(string, s[j]);
    111 	    strcpy(s[j], s[j + 1]);
    112 	    strcpy(s[j + 1], string);
    113 
    114             /* swap the numbers */
    115 	    num = n[j];
    116 	    n[j] = n[j + 1];
    117 	    n[j + 1] = num;
    118 	 }
    119 
    120       }
    121    }
    122 } /* eof orderall */
    123