Subversion Repositories wimsdev

Rev

Rev 13928 | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. /*
  2. *********************************************************************************
  3. * J.M. Evers 6/2005                                                             *
  4. * This is all amateur scriblings... So no copyrights.                           *
  5. * This source code file, and compiled objects derived from it,                  *
  6. * can be used and distributed without restriction, including for commercial use *
  7. * No warrenty whatoever                                                         *
  8. *********************************************************************************
  9. */
  10.  
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #define MAX_SIZE 1024
  15.  
  16. int main ( int argc , char *argv[]){
  17.     if( argc < 2 ){
  18.         fprintf(stdout,"error! usage:!exec toascii $your_list_of_items \nReturns a comma separated list of sums of the ascii values of the individual items.\nExample:\ntoascii cat,dog,house\n312,314,548\n");
  19.         exit(0);
  20.     }
  21.  
  22.     char word[MAX_SIZE];
  23.     int i;
  24.     int length;
  25.     int total = 0;
  26.     int cnt = 0;
  27.     char *ptr;
  28.     char *inp;
  29.     inp = argv[1];
  30.     ptr = (char *) strtok(inp,",");
  31.     while ( ptr != NULL ){
  32.         length = strlen(ptr);
  33.         if( length >= MAX_SIZE){
  34.             fprintf(stdout,"error! size of word is bigger than %d\n",MAX_SIZE);
  35.             exit(0);   
  36.         }
  37.         else
  38.         {
  39.             strncpy( word, ptr, length ) ;
  40.             total = 0;
  41.             for( i = 0;i < length ;i++ ){
  42.                 total = total + word[i];
  43.             }
  44.         }
  45.         if( cnt == 0 ){ cnt = 1; fprintf(stdout,"%d",total);}else{ fprintf(stdout,",%d",total); }
  46.         ptr = (char *)  strtok(NULL, ",");
  47.     }
  48.     fprintf(stdout,"\n");
  49.     return 0 ;
  50. }
  51.  
  52.