Problem solved. TY
Problem solved. TY
I have the general concept in my head, but don't know how you'd go about actually coding it. You can input each word into a matrix with a function that'd count how many letters there are = z. If the position of the matrix of the letter = 0 (or is it 1?) and z, skip and send to output. Else, send to a randomizer function which would randomize the remaining position #s and output the letter while striking that number from the next possible reiteration of the randomizing function.
I remember for the final project of my c++ class in highschool, we had to draw and then animate a logo, bouncing it around the screen. I pretty much slept through class everyday so I just had a stationary logo drawn the best I could and just took my F lol.
tl;dr, do you own homework![]()
The most basic way you could go about it would be to determine if the letter is lowercase or uppercase, and modify the hex value by just adding / subtracting a number between 10 & 20. You could also do a tolower on the letter, and do something like subtract the squareroot of the position to the lowercase letter and you'll get a random caps. The only thing you need to watch is the overlap between Z and z when adding/subtracting.
ASCII Table should make this clearer:
http://www.cs.utk.edu/~pham/ascii_table.jpg
could set the first letter int he sentence (0) to true value, but idk how i would set the last letter to static, or the first/last letters of the rest
While you parse each word from the text file, you can set up a simple swap function to swap the letters around.
After some re-reading, since you want to jumble the letters for each individual word and not a pseudo-encrypt (as I previously thought). As mentioned above, the other way I thought about it was the following:
(for this example, assume I parse 'According' into inputWord to save time/space)
Code:string inputWord; // current input word char inputArray = inputWord.c_str(); // input as C_STR char* tokPtr; // token pointer to parse using strtok int i = 0; tokPtr = strtok(inputArray," "); while(tokPtr != NULL) { srand(time(NULL)); int r = rand()%strlen(inputArray); // obtain a position inside the word // Swap Char base on i; i++; }
Wasn't a real study anyway, just one of those random internet hoaxes that seems to last forever: MRC CBU, Cambridge » Matt Davis
Not going to help the OP with his question in much depth, because it's very obviously homework, but here are the things you need to figure out how to do:
- How to open and read from a text file
- How to write things back out that you read in (whether it's just to the screen or to another text file)
- How to read one word at a time, instead of reading the whole file.
- How to shuffle the non-first and non-last letters of each word before writing it out. (There's a freebie for you, the wikipedia page pretty much hands you the code).
am i on the right track?
In theory this shouldCode:#include <iostream> #include <fstream> using namespace std; void main() { ifstream fin("input.txt"); string s; while( fin >> s ) { cout << "Read from file: " << s << endl; }
read the text file then single out each individual word the file im reading from is "input.txt" and contains:
According to research at an English university, it does not matter in what order the letters in a word are,
the only important thing is that the first and last letter are in the right place. The rest can be a total mess
and you can still read it without a problem. This is because we do not read every letter by itself but the word
as a whole and the brain figures it out anyway.
I think you're code should work fine, while there's input. However, there's a slightly safer way to read through the file, that i've put below. I only recommend using strtok because you can tell it what seperators you do and do not want. if you do it your way I can see a ',' being jumbled into a word eventually.
Code:#include <fstream> #include <iostream> using namespace std; int main (void) { ifstream inputFile; inputFile.open("input.txt"); if(inputFile.is_open()) { while(!inputFile.eof()) { /* * -here is where you would parse the file * -strtok works great for grabbing single words * -getline or << works for single lines */ } } else { cout << "File Not Found\n"; } inputFile.close(); return 0; }
awesome thanks, i'll try that then. now all i gota do is jumble the letters then print them out to the OP file. to jumble the letters would i put the jumble code in the if statement? or make another new function for it.
In my opinion, I would parse each word then call a 'jumble' function that takes a string and returns a jumbled string. Something like;
string jumbleMyNuggets(string nuggets) { ... }
So when you're inside the loop readnig through the file, it's just read a word -> jumble -> re-output the jumbled word.
edit: here is a solution that worked for a short test file I tried. I can't guarentee it will work for yours and im pretty sure I'm still scrambling the punctuation, but I coded this in like 15 minutes so i apologize for the sloppy code. Here is a 'general' idea of how one could do it. I would definitely not copypasta this since i haven't debugged it at all. However, the idea is there so I hope it helps.
Code:#include <string> #include <time.h> #include <fstream> #include <iostream> using namespace std; int main (void) { string inputWord; ifstream inputFile; inputFile.open("input.txt"); if(inputFile.is_open()) { while(!inputFile.eof()) { inputFile >> inputWord; char* str = new char[inputWord.size()+1]; copy(inputWord.begin(),inputWord.end(),str); str[inputWord.size()] = '\0'; srand(time(NULL)); int strLength = strlen(str); for(int i = strLength; i >= 0; i--) swap(str[rand()%strLength],str[i-1]); // Output Jumbled Word string convert; convert = convert + str; cout << convert << ' '; delete str; } } else { cout << "Error: File Not Found\n"; } inputFile.close(); return 0; }
dont apologize gives me something to refrence to. do i need to # include anything special for the copy and swap functions? I log errors saying undefined function 'copy' in function main(), and could not find a match for 'swap<charT,traits,Allocator> <char, char>'
also is your swap function a set code? or did you create it yourself, meaning like did you just come up with it or is it widley used (like the timestamp function). also to right the jumbled words to a output fille all i would do is change
to a code to write it to a OP file called like output.txt right?Code:string convert; convert = convert + str; cout << convert << ' ';
I want to get yours working and doing everything right then go back and make a totaly new one using yours as refrence.
You might be using GCC to compile instead of G++, or you may be compiling incorrectly if that doesn't build. g++ -o jumble jumble.cpp is how you could build the file. Aside from that, the code I posted builds. The swap function is just a char array function to swap array positions in the c string.
Lastly, the string called convert is my shortcut to translate a pointer into a string to use cout. if you were outputting to a file, you could just use the output file name. Looking at my code some more, I would probably use strtok instead so I don't jumble commas inside the words. Again though, thats the general idea and I'm sure you could develop a good lab off it as reference.
im using borland complier. onlything these computers have installed on them other than visual basics.
just going into my file and typing bcc32 lab5.cpp like usual
is this the right way to write to the info to a new file called Output.txt?
Code:#include <string> #include <time.h> #include <fstream> #include <iostream> #include <ifstream> using namespace std; int main (void) { string inputWord; ifstream inputFile; inputFile.open("input.txt"); inputFile.open("output.txt"); if(inputFile.is_open()) { while(!inputFile.eof()) { inputFile >> inputWord; char* str = new char[inputWord.size()+1]; copy(inputWord.begin(),inputWord.end(),str); str[inputWord.size()] = '\0'; srand(time(NULL)); int strLength = strlen(str); for(int i = strLength; i >= 0; i--) swap(str[rand()%strLength],str[i-1]); // Output Jumbled Word string convert; convert = convert + str; inputFile << convert << endl; delete str; } } else { cout << "Error: File Not Found\n"; } inputFile.close(); return 0; }
http://i522.photobucket.com/albums/w..._515/error.jpg
the first part with errors is with swap and copy in the code, if i flat delete it from the code it does compile, although i get the printed results
I wuuub programming. Some quick comments:
need to change this toCode:ifstream inputFile; inputFile.open("input.txt"); inputFile.open("output.txt");
ofstream outputFile;
outputFile.open("output.txt");
You never want to srand inside a loop. Do it once at the top of your program to seed the RNG then leave it alone. This is also known as "How people think SE's RNG works" (reinitializing the rng within the loop on a fast program essentially just reseeds it to the same number repeatedly since time(NULL) returns a value only accurate to the second, so on a program that can be expected to complete in 1-2 seconds, most of the values returned by rand() would end up being the exact same)Code:srand(time(NULL));
Not sure why you're stepping through the array backwards,Code:for(int i = strLength; i >= 0; i--) swap(str[rand()%strLength],str[i-1]);
but when i hits 0 (i>=0) then str[i-1] is going to be negative (aka very large)
Shouldn't close a file handle unless you're sure it's opened, so you should close it inside the control statement that checks that it's open.Code:inputFile.close();
As mentioned above, the way you're coding it won't properly account for punctuation. I'm not sure why you're creating a character array either since you can directly manipulate the string (after all, that's what it's there for).
Plus you need to account for not wanting to mix up the first and last letters, you'd really want something like this:
The above should properly handle short words (will always swap the middle two letters of a four-letter word, won't swap anything in a word with 3 letters or less) and, in fact, guarantees that no letter ends up in its original position (although two of the same letters could be swapped).Code:void main (void) { string inputWord; ifstream inputFile; inputFile.open("input.txt"); srand((unsigned int)time(NULL)); if(inputFile.is_open()) { while(!inputFile.eof()) { inputFile >> inputWord; for(int i=1;i<(int)inputWord.length()-2;i++) swap(inputWord[i],inputWord[i+(rand()%(inputWord.length()-(2+i)))+1]); cout << inputWord<<" "; } cout <<endl; inputFile.close(); } else cerr << "Error: File Not Found"<<endl; }
Of course, the above code doesn't output to a file, take into account punctuation, or deal with spaces/tabs properly (internal formatting is always reduced to a single space), but it handles the basic functionality.
(oh, and there's some typecasting in the above code that may not be neccesary, but 64-bit time_t throws warnings on my system and string::length() is unsigned, which doesn't work for the comparison if the string is short. I could wrap it with an if(length()>3) since short strings don't get swapped, but this way works too unless your string is like 2 billion characters long).
thx that helps but i get the same problem with your code ><
could not find a match for swap
Code:for(int i=1;i<(int)inputWord.length()-2;i++) { // swap(inputWord[i],inputWord[i+(rand()%(inputWord.length()-(2+i)))+1]); int offset=i+(rand()%(inputWord.length()-(2+i)))+1; char temp=inputWord[i]; inputWord[i]=inputWord[offset]; inputWord[offset]=temp; }