Here's what I want it to do:
the left column represents the new string being assigned, the right is the source, before the digits are rearranged (that would work only if it was 15 characters long):
For example: sOutput[1] has the same value as sInput[3]... My method for doing this is as follows:Code:sOutput Value sOutput[x] sInput[x] 0 1 1 3 2 5 3 7 4 9 5 11 6 13 7 15 8 0 9 2 10 4 11 6 12 8 13 10 14 12 15 14
All that works fine... unless you input a string at least 10 characters (0123456789 won't work, but 123456789 will.)Code:void scramble() { char sInput[1024]; char sOutput[1024]; int iLength = 0; int i = 0, x = 0, y = 0, z = 0; cout << "What message do you want to scramble?\n"; cin.get (sInput, 1024); cin.ignore(1024, '\n'); iLength = strlen( sInput); strrev(sInput); x = 1; for (i = 0; (i<iLength) && (sInput[x] != '\0'); i++) { sOutput[i] = sInput[x]; x = x + 2; } x = 0; for (i; (i<iLength) && (sInput[x] != '\0'); i++) { sOutput[i] = sInput[x]; x = x + 2; } sOutput[i] = '\0'; cout << sOutput << endl << endl; // 1 2 3 4 5 is an input example // 1 3 5 2 4 is the scrambled example // 4 2 5 3 1 is what it looks like after strrev }
If you input 10 or more characters, it gives you mostly garbage characters.
Also, I can't think of a simple way to descramble that so that 42531 becomes 13524 (strrev would work for that part), but then I need 13524 to be able to become 12345... so it would work both ways.



LinkBack URL
About LinkBacks




I tried!