Rev 3688 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
3688 | schaersvoo | 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 | ********************************************************************************* |
||
13928 | schaersvoo | 9 | |
10 | 7/5/2019 use strlcpy, strlcat - size-bounded string copying and concatenation |
||
3688 | schaersvoo | 11 | */ |
12 | |||
13 | #include <stdio.h> |
||
14 | #include <stdlib.h> |
||
15 | #include <string.h> |
||
16 | #define MAX_SIZE 1024 |
||
17 | |||
18 | int main ( int argc , char *argv[]){ |
||
19 | if( argc < 2 ){ |
||
20 | 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"); |
||
21 | exit(0); |
||
22 | } |
||
23 | |||
24 | char word[MAX_SIZE]; |
||
25 | int i; |
||
26 | int length; |
||
27 | int total = 0; |
||
28 | int cnt = 0; |
||
29 | char *ptr; |
||
30 | char *inp; |
||
31 | inp = argv[1]; |
||
32 | ptr = (char *) strtok(inp,","); |
||
33 | while ( ptr != NULL ){ |
||
34 | length = strlen(ptr); |
||
35 | if( length >= MAX_SIZE){ |
||
36 | fprintf(stdout,"error! size of word is bigger than %d\n",MAX_SIZE); |
||
37 | exit(0); |
||
38 | } |
||
39 | else |
||
40 | { |
||
13928 | schaersvoo | 41 | strlcpy( word, ptr, sizeof(word) ) ; |
3688 | schaersvoo | 42 | total = 0; |
43 | for( i = 0;i < length ;i++ ){ |
||
44 | total = total + word[i]; |
||
45 | } |
||
46 | } |
||
47 | if( cnt == 0 ){ cnt = 1; fprintf(stdout,"%d",total);}else{ fprintf(stdout,",%d",total); } |
||
48 | ptr = (char *) strtok(NULL, ","); |
||
49 | } |
||
50 | fprintf(stdout,"\n"); |
||
51 | return 0 ; |
||
52 | } |