-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathwordmixer.cpp
More file actions
78 lines (64 loc) · 1.32 KB
/
wordmixer.cpp
File metadata and controls
78 lines (64 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
* Wordmixer
* June 24, 2010
* William Chen
*
* Reads in a file and changes the order of letters within a word,
* except for the beginning and ending character. Outputs to out.txt
*/
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <time.h>
#include <string.h>
using namespace std;
int main( int argc, char** argv )
{
char* filename = (char *) "output.txt";
if( argc < 2 ) {
printf( "Usage: %s inputfile.txt [options]\n", argv[0] );
printf( "\nOptions:\n" );
printf( " -o\toutput file name\n");
exit(1);
}
else if( argc > 2 ) {
if( strcmp(argv[2], "-o")==0 ) {
filename = argv[3];
}
else {
printf( "Invalid option.\n" );
exit(1);
}
}
srand( time(NULL) );
ifstream infile;
ofstream outfile( filename );
infile.open( argv[1] );
if( !infile.good() ) {
printf( "Bad input filename: %s\n", argv[1] );
}
while( !infile.eof() ) {
string word;
infile >> word;
int len = word.length();
if( len <= 3 ) {
outfile << word << ' ';
continue;
}
for( int k=1; k<len-2; k++ ) {
int random;
if( k < len-3 )
random = rand() % (len-k-2) + k;
else if( k == len-3 )
random = k+1;
char old = word[k];
word[k] = word[random];
word[random] = old;
}
outfile << word << ' ';
}
infile.close();
outfile.close();
return 0;
}