1 /* Scramble the "inner" letters of each word in the input into a random order, and output the result. Non-word (that is, non-alphabetical) characters, and the first and last letters of each word, are left alone.
2 * Output to something other than stdout will append to the file instead of overwriting it - this may be undesirable, and can be changed if so.
5 /* Copyright 2009-07-11 Andrew J. Buehler.
8 /* This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include <ctype.h> // for isalpha()
22 #include <stdlib.h> // for malloc()/calloc()/realloc()/free() and rand()/srand()
23 #include <string.h> // for strlen()
24 #include <time.h> // for time()
26 #define ALLOW_FILE_IO 0
29 /* strips 'reduction' characters from the end of the input string and returns the result
30 * works only if strlen(string> >= reduction, which is the case in the only place it is presently called
32 char *shorten_string(char *string
, int reduction
) {
37 for (; reduction
> 0; reduction
--, i
--) {
38 string
[i
-1] = '\0'; // would it work to use an 'i-reduction' approach instead, similar to what was later done in narrow_string()?
44 /* strips 'reduction' characters from the beginning and the end of the input string and returns the result
45 * works only if(strlen(string) >= 2*reduction), which is the case in the only place it is presently called
47 char *narrow_string(char *string
, int reduction
) {
51 string
[i
-reduction
] = string
[i
];
54 string
[i
-reduction
] = '\0';
56 return shorten_string(string
, reduction
);
59 int all_one_letter(char *string
) {
64 for(i
= 1; string
[i
] != '\0'; i
++) {
66 return 0; // a nonduplicate letter has been found
71 return 1; // reached the end of the string having found only duplicate letters, so it's all one letter
74 /* randomly reorders the contents of the string
75 * WARNING: frees the input string and returns a replacement
77 char *scramble_string(char *string
) {
82 if(len
< 2) return string
; // can't scramble a 1-character string or an empty string!
83 if(all_one_letter(string
)) return(string
); // can't scramble a string which consists entirely of one letter!
87 while(strcmp(string
, ret
) == 0) {
89 tmpstr
= strdup(string
);
94 while(tmpstr
[i
] != '\0') {
95 tmpstr
[i
] = tmpstr
[i
+1];
101 len
= strlen(string
);
109 char *clear_string(char *string
) {
114 for(; i
>= 0 ; i
--) {
121 int main(int argc
, char **argv
) {
123 char c
, tempchar
, *word
;
124 FILE *infile
, *outfile
;
127 /* open files, if any other than stdin and stdout */
129 if(!strcmp(argv
[1], "--help") ||
130 !strcmp(argv
[1], "-h")) {
131 printf("Usage: %s [INPUT_FILENAME] [OUTPUT_FILENAME]\n", argv
[0]);
132 printf("If INPUT_FILENAME is omitted or is '-', read from standard input\nIf OUTPUT_FILENAME is omitted or is '-', print to standard output\n");
136 if(strcmp(argv
[1], "-")) {
137 infile
= fopen(argv
[1], "r");
139 fprintf(stderr
, "Unable to open input file %s for reading\n", argv
[1]);
147 if(strcmp(argv
[2], "-")) {
148 outfile
= fopen(argv
[2], "a");
149 if(outfile
== NULL
) {
150 fprintf(stderr
, "Unable to open output file %s for writing\n", argv
[2]);
157 } else { // no arguments specified
166 srand(time(NULL
)); // needed for scramble_string() to actually be random
169 word
= malloc(sizeof(char));
174 printf("Reached EOF while reading the first character of the input file!\n");
178 while(!feof(infile
)) {
180 word
= realloc(word
, word_length
+2); // one for the new character, one for the null
181 word
[word_length
] = c
;
182 word
[word_length
+ 1] = '\0'; // duplicate addition with the next line, but possibly more readable
187 fputc(word
[0], outfile
);
189 tempchar
= word
[word_length
];
190 word
= scramble_string(narrow_string(word
, 1));
191 fprintf(outfile
, "%s", word
);
192 fputc(tempchar
, outfile
);
194 word
= clear_string(word
);
This page took 0.02712 seconds and 4 git commands to generate.